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:
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?
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.
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?