Switch toggle icon return to position "off"

Hello everybody, i have a problem with switch. When I turn on icon, it still come back to position 0 after 5sec. I have no errors in log and request send succefull becouse output is on. When I click icon during this 5 sec, command_off request send succefully too, becouse output is off. What I must do to work it correctly? And is it posible icon automaticly refresh actual state of output when it trun on/off out from home assistant interface? Device is eaton easy e4 and i want communicate it via json API. Thats my code

- platform: command_line
  switches:
    sauna1:
      friendly_name: sauna test
      command_on: 'python3 /config/scripts/sauna_on.py'
      command_off: 'python3 /config/scripts/sauna_off.py'
      command_state: 'python3 /config/scripts/sauna_req.py'
      value_template: >
        {% if [V] == '1' %} mdi:toggle-switch
        {% else %} mdi:toggle-switch-off
        {% endif %}

response from command_state script is:

{"OPERANDS":{"OSINGLE":[{ "INDEX":2,"V":0}]}}

and when “V”:0 state is off, “V”:1 on

request of command_on and command_off havn’t response

Deja Vu… This was my first question when I joined this forum a 5 years ago. (Wow, I’ve been pestering people for a long time…)
I can tell you why, not how to fix it.

Home Assistant is not getting a state update from your device.

Surely the problem is -

  1. The response is JSON, so to start with you need to tell Home Assistant to read it as JSON:
value_json

Second the full path is:

value_json['OPERANDS']['OSINGLE'][0]['V']

[V] means absolutely nothing on it’s own.

Also the result of your value_template is an icon. This is not correct. The value template must resolve to true or false to indicate the state of the switch, true = on, false = off. The switch integration will then change the state of the switch toggle (this is not the icon, the icon is on the left).

- platform: command_line
  switches:
    sauna1:
      friendly_name: sauna test
      command_on: 'python3 /config/scripts/sauna_on.py'
      command_off: 'python3 /config/scripts/sauna_off.py'
      command_state: 'python3 /config/scripts/sauna_req.py'
      value_template: "{{ value_json['OPERANDS']['OSINGLE'][0]['V'] == 1 }}"

If you want to change the icon based on the state of the switch use an icon template as well:

- platform: command_line
  switches:
    sauna1:
      friendly_name: sauna test
      command_on: 'python3 /config/scripts/sauna_on.py'
      command_off: 'python3 /config/scripts/sauna_off.py'
      command_state: 'python3 /config/scripts/sauna_req.py'
      value_template: "{{ value_json['OPERANDS']['OSINGLE'][0]['V'] == 1 }}"
      icon_template: >
        {% if is_state('switch.sauna1', 'on') %} 
          mdi:radiator
        {% elif is_state('switch.sauna1', 'off') %} 
          mdi:radiator-off
        {% else %} 
          mdi:alert
        {% endif %}

Perhaps true in some cases, but I had the same issue with an input_boolean. (And other)
In my case it was a custom card or custom ui (not updated) that was the problem.

Just wanted to open up for other reasons, not saying you are wrong.
If it’s only this entity then I agree with you Stephen but if it happens to more entities then it’s probably something else.

Nope. It’s definitely this as explained by Andrew and myself above.

Thank You man!, it solution for my problem :slight_smile: i’m using HA from few days and not every is logic for me yet :slight_smile:

1 Like