I have a sensor which gets propagated to HA through mqtt/json. Name it “/heizung/r_uwp”.
It can be controlled through another MQTT channel (“/heizung/cmd”). With the above automation I am able to set the value (ON/OFF resp 1/0).
Now I want to have a controlling element in HA to switch this device on or off. So I cwas thinking to create a (dummy) device which fires the automation when enabled or disabled.
First question arises- how do I create such a dummy device? When trying to add through GUI it always asks me for a vendor…
My second question now is how do I sync the sensor and the switch state? What happens when the message gets lost “on the way”- the sensor shows 0 while the device says 1.
Can I somehow set the device to to the value of the sensor (with a little delay…)?
The turn_on and turn_off actions can be a list of actions so no need for an automation. Use the value_template option to obtain the current state of the switch from your sensor.
Now fiddling around and trying to get it work.
Unfortunately unsuccessful up to now
However, I have installed the template-helper and I created a switch based on it through the UI. (which does not offer to show the full YAML code…). In which configuration file is it stored?
However, I added the value template for the current state of the sensor: {{ is_state('sensor.r_pump', 1) }}
Now I added the automation as action for switching on:
If you don’t need a trigger then use a script, not an automation. However, as I said you don’t need an automation.
Template switch:
# Example configuration.yaml entry
switch:
- platform: template
switches:
pump:
friendly_name: Pump
value_template: "{{ is_state('sensor.r_pump', '1') }}"
turn_on:
- action: mqtt.publish
data:
qos: "0"
retain: false # it is a very bad idea to retain commands. You will get "ghost' switching if you do this.
topic: hzg/command
payload_template: >
{ "name": "hzghaos", "unixtime": {{ as_timestamp(now()) }}, "status": {
"heizung": true, "manuallen:" 5,"readcfg":false}, "relais":{"r_pump": {{
states('switch.schalter_umwalzpumpe') }} }
turn_off:
- action: mqtt.publish
data:
qos: "0"
retain: false
topic: hzg/command
payload_template: >
{ "name": "hzghaos", "unixtime": {{ as_timestamp(now()) }}, "status": {
"heizung": true, "manuallen:" 5,"readcfg":false}, "relais":{"r_pump": {{
states('switch.schalter_umwalzpumpe') }} }
True and false appear more than once in your payload. You did not explain which one to change. So the templates in the above turn on and turn off actions are the same. Change one of them.
perfect! Thank you so much for your above input.
I configured it now as shown below. For the as_timestamp I had to add the integer stuff and fix some minor tyypos. But working now: