#post all your code and the error
1 messages · Page 1 of 1 (latest)
automation:
- id: Pre-set points card scanner
alias: "Pre-set points card"
mode: restart
# Hide warnings when triggered while in delay.
# max_exceeded: silent
trigger:
platform: event
event_type: tag_scanned
condition:
- "{{ is_state('switch.points_group_switches', 'on') }}"
action:
- service: shell_command.manage_cards
data:
args: "--info {{trigger.event.data.tag_id}}"
response_variable: get_info_response
- variables:
UsersCount_json: "{{ (get_info_response['stdout'] | from_json or 'x' | default({})).user |count }}"
CardCount_json: "{{ (get_info_response['stdout'] | from_json or 'x' | default({})).card |count }}"
User: "{{ (get_info_response['stdout'] | from_json or 'x' | default({})).user | default('') }}"
Card: "{{ (get_info_response['stdout'] | from_json or 'x' | default({})).card | default('') }}"
Modify: "{{ (get_info_response['stdout'] | default('{modify_user: {user_info:{}}}')|from_json).modify_user.user_info}}"
User_Name: "{{ (User or Modify).user_name }}"
user_id: "{{ (User or Modify).user_id }}"
user_points: "{{ (User or Modify).user_points }}"
activation_status: "{{ (User or Modify).activation_status }}"
Card_Points: "{{ Card.points }}"
Card_Status: "{{ Card.status }}"
and the error Error: UndefinedError: 'dict object' has no attribute 'modify_user'
I see the error in "traces"
It is stopping me from having anything else further in the automation...
make a variable that does the operation
variables:
response: >
{{ get_info_response['stdout'] | from_json }}
then every time you want to access something in it, use .get
and if it fails, use
k
variables:
response: >
{{ get_info_response['stdout'] | from_json or {} }}
then {{ response.get('whatever key', {}) }}
- variables:
response: "{{ get_info_response['stdout'] | from_json or {} }}"
user: "{{ response.get('user', []) }}"
UsersCount_json: "{{ user | count }}"
card: "{{ response.get('card', []) }}"
CardCount_json: "{{ card | count }}"
modify: "{{ response.get('modify_user', {}) }}"
modify_user: "{{ modify.get('user_info', {}) }}"
etc
notice how much easier it is to follow and you aren't copying/pasting the same crap over and over again
also, "{{ (User or Modify).user_name }}" seems wrong because the way your code is written, it seems that user will be a list. If it's not, why are you counting the key value pairs for user?
users is a list, I want the count for other parts of the automation.
I think I did something wrong:
Template variable error: 'modify_user' is undefined when rendering '{{modify_user.get('user_info', {})}}'
- variables:
response: "{{ get_info_response['stdout'] | from_json or {} }}"
UsersCount_json: "{{response.get('user', {})|count}}"
CardCount_json: "{{response.get('card', {})|count}}"
Card: "{{response.get('card', {})}}"
User: "{{response.get('user', {})}}"
Modify_user: "{{response.get('modify_user', {})}}"
Modify: "{{modify_user.get('user_info', {})}}"
here is a sample of the possible json responses.
{
"users": [
{
"user_id": "8243",
"user_name": "William",
"user_points": "123",
"activation_status": "active",
"tags": [
"987654"
]
},
{
"user_id": "243",
"user_name": "Alise",
"user_points": "611",
"activation_status": "active",
"tags": [
"123456"
]
}
]
}
{
"modify_user": {
"action": "Modify User",
"user_info": {
"user_id": "8243",
"user_name": "William",
"user_points": "333",
"activation_status": "active",
"tags": [
"987654"
]
}
}
}
or exceptions for failures.
Thanks but that still doesn't really help because you have a list of users vs a modify_user
do you want to use the modify_user if the list is empty?
I am confusing myself too.
I think I got this step working.
I don't want to use anything that is empty, i just want a generic script/automation to call the shell command and set any variables i will need.
so if the user list is empty, you want to use the modify user?
i get either a users list of users like in the first json or a response that a user was modified.
In both scenarios I want to know the user_name and the user_points.
There is also a cards list of cards similar to users list
what's the user name of 7 users?
hopefully you're starting to see the holes in this plan. Or maybe you can share the endgoal with the data.
the endgoal is to
A. scan a tag and lookup in a json file the users name and points and update a input_number based on the username and userpoints.
B. scan a tag and then scan a new tag (points_card) that has pre-set points stored on it, and pass that info to the shell_command in the format --use <tag> <user> and get the response in form of modify_user json and update points like in A.
So this is an all in 1 automation?
doing both things?
I still don't see what the list is going to do. are you planning on running a shell command for each user in the list?
and what are the possible responses from the shell command?
is it just the 2 and no response (empty string)
i've been thinking about your advice (see the holes in this plan) and i am trying to figure out myself what I want to happen exactly in small pieces first and then if possible put into all-in-one script.
here is why I wanted the list of users to do. (so far its working)
update_points:
alias: "read points from file and set input numbers"
sequence:
- service: shell_command.manage_cards
data:
args: "--list_users all"
response_variable: manage_points_response
- choose:
- conditions: "{{ manage_points_response is defined }}"
sequence:
- variables:
Users: "{{ (manage_points_response['stdout'] | from_json | default({})).users}}"
Count_json: "{{ Users |count}}"
- repeat:
count: "{{ Count_json }}"
sequence:
- variables:
user_entity_id: "{{ Users[repeat.index - 1].entity_id }}"
user_points: "{{ Users[repeat.index - 1].user_points | int }}"
- choose:
- conditions: "{{ states(user_entity_id)!= 'unknown' }}"
sequence:
- service: input_number.set_value
data:
entity_id: "{{ user_entity_id }}"
value: "{{ user_points }}"
obviously if the amount of users gets to be too much i will need to do this on the shell side of things, but there shouldn't be more than 10 more likely 3-5.
You should use for_each instead of count, and use if then if you only are doing 1 thing to cut down on the yaml.