Sensor as Switch?

Hi,

following up the automation update through MQTT I am thinking how to trigger this automation.

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…)?

Thanks for input!

/KNEBB

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.

1 Like

Hi,

thanks, I found this, too.

Now fiddling around and trying to get it work.
Unfortunately unsuccessful up to now :frowning:

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:

action: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.mqtt_publish

But I am unsure how to tell my automation to switch it on (or off). Or do I need to create two automations for this?
This is the automation:

alias: MQTT Publish
description: ""
trigger: []
condition: []
action:
  - action: mqtt.publish
    data:
      qos: "0"
      retain: true
      topic: hzg/command
      payload: >-
        { "name": "hzghaos", "unixtime": {{ as_timestamp(now()) }}, "status": {
        "heizung": true, "manuallen:" 5,"readcfg":false}, "relais":{"r_pump": {{
        states('switch.schalter_umwalzpumpe') }} }
mode: single

How can I use this automation to use either “true” or “false”? The current states() will use current state and therefor not change anything.

Additional issues, the mqtt sensor will update its state with a delay of up to 5 mins…

Any further ideas?

/KNEBB

Dont ever do this:

alias: MQTT Publish
description: ""
trigger: []
condition: []

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.

Hi there,

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:

switch:
  - platform: template
    switches:
      pump:
        friendly_name: "Heizung Umwälzpumpe"
        value_template: "{{ is_state('sensor.r_pump', '1') }}"
        turn_on:
          - action: mqtt.publish
            data:
              qos: "0"
              retain: false
              topic: hzg/command
              payload: >
                { "name": "hzghaos", "unixtime": {{ as_timestamp(now()) | int(0) }}, "status": {
                "heizung": true, "manuallen": 35,"readcfg":false}, "relais":{"r_pump": true } }
        turn_off:
          - action: mqtt.publish
            data:
              qos: "0"
              retain: false
              topic: hzg/command
              payload: >
                { "name": "hzghaos", "unixtime": {{ as_timestamp(now()) | int(0) }}, "status": {
                "heizung": true, "manuallen": 35,"readcfg":false}, "relais":{"r_pump": false } }

Thanks a lot!

/KNEBB

1 Like