Switch through MQTT with Delay?

Hi,

I have configured a switch which can control a device through MQTT as follows:

  - platform: template
    switches:
      wp:
        friendly_name: "Wärmepumpe"
        value_template: "{{ is_state('sensor.r_wp', '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": 62,"readcfg":false}, "relais":{"r_wp": 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": 62,"readcfg":false}, "relais":{"r_wp": false } }

Works really good so far. Unfortunately, the device will check for updates only every five minutes. And send updates (through MQTT) only then. So the state of the switch will go back to “off” on the GUI after a couple of seconds (because the device itself stays in “off”). Only when the device checks for new commands it will update the state.

Do I have a change to set the state somehow manually until the next scheduled update?

Thanks for ideas!

/KNEBB

The state of the switch (an its update frequency) comes from sensor.r_wp, not from the switch itself.

You could use an intermediary input_boolean helper to mitigate the slow updates (set it to true/false in turn_on/turn_off, and also on the update of sensor.r_wp)

Ok, sounds like a solution, indeed. Here is the documentation for this.

But I am struggling (again) with the syntax of how to use it. :frowning:
I added the following to my configuration.yaml:

template:
  - number:
    - name: "LHzR"
[...]
  - input_boolean:
      updater_ehzg:
        name: "Helper EHzg"

But it complains about it:
Invalid config for 'template' at configuration.yaml, line 46: 'input_boolean' is an invalid option for 'template'

So how do I configure it? Do you have an example?

Thanks!

/KNEBB

An input_boolean is a helper, created from the GUI

1 Like

Well, I bet it can be configured through .yaml, too.

At least I expected to find an invocation of the helper (generated through GUI) somewhere in cofngiuration file. But I do not :face_with_raised_eyebrow:

However, I have my helper now. But how do I configure my switch to add this action?
Trying to add a second action does not work:

      ehzg:
        friendly_name: "Elektroheizstab"
        value_template: "{{ is_state('sensor.r_ehzg', '1') }}"
        turn_on:
          - action: mqtt.publish
            data:
              qos: "0"
              retain: false
              topic: hzg/command
              payload: >
                { "name": [...] } }
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.helper_ehzg
  1. Use the helper as the value of the switch
{{ is_state('input_boolean.helper_ehzg', 'on') }}
  1. Create an automation that will set the value of the helper when the actual sensor value changes

Schematically (syntax not checked)

triggers:
  - platform: state
    entity_id: sensor.r_ehzg
actions:
   - action: input_boolean.turn_{{ iif(trigger.to_state.state == '1', 'on', 'off') }}
     target:
       entity_id: input_boolean.helper_ehzg
1 Like

Hi,

fighting for two days how to configure this. Up to now I did not succeed. :frowning:

I have configured my helper in the GUI (do not find an invocation in the yaml files :face_with_raised_eyebrow: ):

I configured the switch helper which uses the above helper for its state:

      ehzg:
        friendly_name: "Elektroheizstab"
        value_template: "{{ is_state('input_boolean.helper_ehzg', 'on') }}"
        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": 70,"readcfg":false}, "relais":{"r_ehzg": 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": 70,"readcfg":false}, "relais":{"r_ehzg": false } }

Then I created two automations (only shown the “on” version):

alias: Helper E_Hzg (on)
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.r_ehzg
    from: "off"
    to: "on"
conditions: []
actions:
  - action: input_boolean.turn_off
    target:
      entity_id: input_boolean.helper_ehzg
    data: {}
mode: single

What I get from these are two “switches” on the gui where I can enable or disable the device in question.
When setting the switch helper to “on”, it sends the MQTT message but nothing happens to the boolean helper (of course, because the sensor has not changed its state due to 5min delay).
When using the boolean helper to switch, the switch helper immediately follows the setting of the boolean helper regarding its state. But it does not send the MQTT messages …

I do not mind using only one of these two on the dashboard but it should follow the other…

Any further ideas?

/KNEBB

From my understanding it would be perfect if I could configure an additional action to my mqtt switch which sets the boolean helper to on or off. But as far as I understand I can only have a single action there.

So I tried to use a script for it:

alias: Update E_Hzg und Helfer
sequence:
  - action: input_boolean.toggle
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.helper_ehzg
  - action: mqtt.publish
    metadata: {}
    data:
      topic: /hzg/command
      payload: >-
        { "name": "hzghaos", "unixtime": {{ as_timestamp(now()) | int(0) }},
        "status": { "heizung": true, "manuallen": 70,"readcfg":false},
        "relais":{"r_ehzg": false } }
description: ""

When executing this script, simply nothing happens… no mqtt message, no state change.

I am lost :frowning:

Tried as suggested to have only a single automation, but failed miserably due to syntax:

alias: Helper E_Hzg (off)
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.r_ehzg
    to: null
conditions: []
actions:
  - action: >-
      input_boolean.turn_"{% if(trigger.to_state.state == '1') %} 'on' {% else
      %} 'off' {% endif %}"
    target:
      entity_id: input_boolean.helper_ehzg
    data: {}
mode: single

gives an error:
Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'

I am still lost here…

No, you can put more then action

1 Like

[Update-Edit] Seems to work with the spaces removed before “on” and “off”. Updated to document a working example.

Ok, thanks for clarification. I tried it now with a single automation.
Expected behaviour is on a state change of the sensor to start an according (on or off) action to set the helper, too.

But, getting errors :frowning:

lias: Helper E_Hzg Status
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.r_ehzg
    to: null
conditions: []
actions:
  - action: >-
      input_boolean.turn_{% if(trigger.state == '1') %}on{% else %}off{%
      endif %}
    target:
      entity_id: input_boolean.helper_ehzg
    data: {}
mode: single

Getting error when executing the automation:
Template rendered invalid service: input_boolean.turn_ off

- platform: template
    switches:
      wp:
        friendly_name: "Wärmepumpe"
        value_template: "{{ is_state('input_boolean.helper_ehzg', 'on') }}"
        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": 62,"readcfg":false}, "relais":{"r_wp": true } }
          - action: input_boolean.toggle
            target:
              entity_id: input_boolean.helper_ehzg
        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": 62,"readcfg":false}, "relais":{"r_wp": false } }
          - action: input_boolean.toggle
            target:
              entity_id: input_boolean.helper_ehzg
1 Like