I’ve got a device I can access via a python script and get a json answer with a status of 8 switches.
This is an example of returned string with a json structure:
Unfortunately I cannot get the attributes to be stored in the sensor. The sensor hasn’t got any attributes at all. I tried to list the attributes with equation marks like - “1” or switch to string like - “a” (with updating the script code accordingly) but nothing works.
Do you know where I make a mistake?
Any suggestion from your side would be very welcome - thank you!
Peter
In order to get a status of all 8 switches integrated in this single device with just one query.
As far as I know switch does not support json attributes.
And I do have config for the switches for example:
- platform: command_line
switches:
itera07:
friendly_name: iteratec07
command_on: >
python3 /config/custom_components/iteratec/switch.py 7 on raw
command_off: >
python3 /config/custom_components/iteratec/switch.py 7 off raw
command_state: >
python3 /config/custom_components/iteratec/switch.py 7 status raw
value_template: '{{ value == "1" }}'
icon_template: >
{% if value == "1" %}
mdi:toggle-switch
{% elif value == "0" %}
mdi:toggle-switch-off
{% else %}
mdi:lightbulb-off
{% endif %}
In this case the script returns just 0, 1 or “unavailable” string (let’s say in a “raw” data format).
What is wrong with the sensor?
Hi Petro, please accept my apologies for bothering you with a direct call
Anyway you motivated me to test the config in more details.
I tested this already with quotes and it did not work, as mentioned in the first post.
The problem however was in the way how I defined the sensor in the config, which I did not share with you to reduce size of the post. So the full sensor code was:
- platform: command_line
name: iteraswitch
command: >
# Call switch with dummy 0 and json .... bla bla
python3 /config/custom_components/iteratec/switch.py 0 status json
json_attributes:
- '1'
- '2'
- '3'
- '4'
- '5'
- '6'
- '7'
- '8'
value_template: >
{% if value == "unavailable" %}
{{ 'off' }}
{% else %}
{{ 'on' }}
{% endif %}
… and the problem is at the line #Call switch with dummy 0 and json … bla bla
Apparently a comment cannot be used at this place. The python script was simply not executed.
When I removed this comment everything started working as designed.
Many thanks for keeping me motivated