Command line switch value_template not updating

switch:
  - platform: command_line
    switches:
      meross_xmas_tree:
        friendly_name: Albero 1
        command_on: "..."
        command_off: "..."
        command_state: ""
        value_template: "{{ is_state('sensor.meross_xmas_tree', 'on') }}"

I have this command line switch which I use to trigger two shell commands to turn on and off a Meross smart plug.
The shell commands write also “on” or “off” in a text file, to which is pointed a file sensor:

sensor:
  - platform: file
    name: meross_xmas_tree
    file_path: "/home/homeassistant/.homeassistant/python_scripts/meross_xmas_tree.txt"

The value_template of switch.meros_xmas_tree is linked to sensor.meros_xmas_tree, thus showing the current state of the plug.
This works great inside Home Assistant. But the problem is that the value_template seems to update only if I toggle the switch from the Home Assistant UI.
If, instead, the shell commands are execute somewhere else, the text file is updated correctly, sensor.meros_xmas_tree is updated correctly, but switch.meros_xmas_tree is not updated and is therefore out of sync with the physical state of the plug and, more surprisingly, with its own value_template.

So what I’m missing? Why value_template doesn’t get updated (without manually toggling the switch) if sensor.meros_xmas_tree does?

1 Like

This problem appears not only for an external execution of the “on/off” shell commands, but also after a restart of Home Assistant.
That’s actually the reason why I’m using a text file to retain the switch value. But after a HA restart, the text file have obviously the correct value, sensor.meros_xmas_tree is updated at startup with the text file value, switch.meros_xmas_tree is not updated.

So, again, the value_template doesn’t get updated with the value of sensor.meros_xmas_tree.

As you have found the command_line switch does not poll the switch for updates except when the command is called. The restful switch does, perhaps you can use that instead?

Or did you see this addon?

1 Like

If you set the command_state empty, the switch will update the state only when you toggle and ha restart.
It has to be returned some value from command_state, value_template has to process from the returned value.

How should I use the restful switch to update the state if the plug is toggled outside Home Assistant?
Should I create a GET response indicating the state? But in this case when this will be executed and updated from home assistant?

Or should I manually set the state of the switch externally, with Hass.state.set?

In my case, it does not get updated at HA restart, because it doesn’t read the value stored in the text file.
It would be sufficient for me to be updated at startup but I didn’t understand how

Home assistant will poll the rest switch regularly.

Try this…

switch:
  - platform: command_line
    switches:
      meross_xmas_tree:
        friendly_name: Albero 1
        command_on: "..."
        command_off: "..."
        command_state: "cat /home/homeassistant/.homeassistant/python_scripts/meross_xmas_tree.txt"
        value_template: "{{ value == 'on' }}"

Formatting please @af950833

OK… Done

1 Like