Automation - wait for the state

Hello,
I wanted to make an automation that will turn on my electric heater (for towels) under certain conditions.
If the heat pump is in DHW mode, then after a long press of the button, rember that and wait for either a corresponding level of energy production from PV or until a cheap electricity tariff occurs and then turn on the heater.

alias: Zigbee2MQTT - Tuya 4-Button Scene Switch2
description: ""
use_blueprint:
  path: Stringer/zigbee2mqtt-tuya-4-button-scene-switch-ts0044.yaml
  input:
    button_one_short_press:
      - type: toggle
        device_id: 89be57ec2d649761b73bcffb0f9d4643
        entity_id: switch.lazienka_lustro
        domain: switch
    switch: sensor.wlacznik_lazienka_action
    button_four_long_press:
      - if:
          - condition: state
            entity_id: sensor.aquarea_mode
            state: DHW
        then:
          - wait_for_trigger:
              - platform: numeric_state
                entity_id: sensor.solaredge_current_power
                above: 2000
                value_template: "10000"
          - type: turn_on
            device_id: b814efe99894e92a15d7f72c832fae52
            entity_id: 3c9baa4b32049beaecba1e62a4b08562
            domain: switch
        else: []
    button_four_short_press:
      - type: turn_off
        device_id: b814efe99894e92a15d7f72c832fae52
        entity_id: 3c9baa4b32049beaecba1e62a4b08562
        domain: switch
    button_four_double_press: 

In this code I have not yet activated the variable( I still do not know how to do it) for the cheap tariff.
On the other hand, it doesn’t work for me .,…

I would like it to run only once, for turning off I have a suitable automation:

alias: Wyłącz po czasie Łazienka gniazdo grzejnik
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.lazienka_gniazdo_grzejnik
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 15
condition: []
action:
  - type: turn_off
    device_id: b814efe99894e92a15d7f72c832fae52
    entity_id: 3c9baa4b32049beaecba1e62a4b08562
    domain: switch
mode: single

In addition, how to make the shutdown time can be set from lovelace?

You’re like overengineering the thing.
Just position an input_boolean following a long-press (the “remember”), then have another automation triggering on the “cheap electricity”, that will check the boolean and turn on the heater if requested.

What are you trying to do with this condition?

- wait_for_trigger:
    - platform: numeric_state
      entity_id: sensor.solaredge_current_power
      above: 2000
      value_template: "10000"

Having a static value in the value_template of a Numeric state trigger will cause it to never trigger.

Hmm,
probably you have right. I assumed that in this entity is a numeric value (int or real )and the measurement unit is appended. I’m just a casual user and don’t analyze it like a programmer :slight_smile: . I used the option to create a visual automation and this path suited me :stuck_out_tongue:

You’re right, but I wanted to encapsulate it in one automation so that in the future when I die someday :slight_smile: , someone could read it better.

Then what should I change to make it work as the

sensor.solaredge_current_power

entity will have a value greater than 2000?

I changed to something like this and it still doesn’t work:

alias: Zigbee2MQTT - Tuya 4-Button Scene Switch2
description: ""
use_blueprint:
  path: Stringer/zigbee2mqtt-tuya-4-button-scene-switch-ts0044.yaml
  input:
    button_one_short_press:
      - type: toggle
        device_id: 89be57ec2d649761b73bcffb0f9d4643
        entity_id: switch.lazienka_lustro
        domain: switch
    switch: sensor.wlacznik_lazienka_action
    button_four_long_press:
      - if:
          - condition: state
            entity_id: sensor.aquarea_mode
            state: DHW
        then:
          - wait_for_trigger:
              - platform: numeric_state
                entity_id: sensor.solaredge_current_power
                above: "2000"
          - type: turn_on
            device_id: b814efe99894e92a15d7f72c832fae52
            entity_id: 3c9baa4b32049beaecba1e62a4b08562
            domain: switch
        else: []
    button_four_short_press:
      - type: turn_off
        device_id: b814efe99894e92a15d7f72c832fae52
        entity_id: 3c9baa4b32049beaecba1e62a4b08562
        domain: switch
    button_four_double_press: []

There is no reason to use an If/then without an else or other non-exclusive actions in the same block… just use a Condition.

In a case like this, where the original trigger event is unrelated to the what you are watching in your wait, you should probably use a Wait template instead of a Wait for trigger.

When an automation gets to a Wait template action, the action evaluates the template, and if true, the script will continue. If not, then it will wait until it is true.

When an automation gets to a Wait for trigger, the action sequence will only continue whenever the triggers fire. In the case of a Numeric state trigger like you had, the trigger will only fire when the value goes from below the set point to above the set point. If the value is already above the set point when the action starts, it does not fire the trigger and the wait does not complete.

It is also important whenever using a wait to set a timeout and continue mode. Without these, you will very likely have automations that seem unpredictable or unreliable.

...
    button_four_long_press:
      - condition: state
        entity_id: sensor.aquarea_mode
        state: DHW
      - alias: "Wait until SolarEdge power above 2000"
        wait_template: "{{ states('sensor.solaredge_current_power') | float(0) > 2000 }}"
        timeout:
          minutes: 10
        continue_on_timeout: false
      - type: turn_on
        device_id: b814efe99894e92a15d7f72c832fae52
        entity_id: 3c9baa4b32049beaecba1e62a4b08562
        domain: switch
....

Wait Documentation

Hello,
This automation does not work. I have set with timeout and without timeout. It does not trigger the switch.
Also I don’t know if I explained the problem properly, because pressing the button is supposed to be a type event:
ok when the right time comes then turn on the heater. That time can come even in 10 hours.
I additionally made an energy tariff sensor and wanted to add this in the “or” condition, meaning either solardedge >2000 or tariff: offpeak

- name: Taryfa  
       state: >
         {% set month = now().strftime('%m') | int %}
         {% set hour = now().strftime('%H') | int %}
         {% set isSummer = (month >= 4 and month <= 9) | bool %}
         {% set isWinter = (month <= 3 or month >= 10) | bool %}
         {% set isWorkday = states("binary_sensor.workday_sensor") | bool %}
         {% if isWorkday and hour >= 6 and hour < 13 %}
           peak
         {% elif isWorkday and (hour >= 15 and hour < 22) %}
           peak
         {% else %}
           off_peak
         {% endif %}

I agree, I thought the goal was that the button press would turn on a towel heater if certain power conditions were met. Please just explain what the purpose of pressing the button is. To me, it makes no sense to say “Warm these towels… whenever”.

That being said, putting an automation into a prolonged wait is not advised. Waits do not survive restarts or reloading of the automation. The length of wait that is reasonable depends on many factors including the stability of your system or how often you tinker with your system and automations.

Your sensor’s template includes two variables, isSummer and isWinter, that don’t do anything.

You make it so complicated that I bet nobody will understand your “single, all-at-once” automation when you die, anyway :smiley:

Hello,
I’m late replying, but I was on vacation. I’m already explaining. Imagine that you take a shower once a day e.g. in the morning. The next shower will be in the evening, or again in the morning. The minimum time distance is, say, 12 hours. The towel by that time should be dry. Well, then we can dry it when it is cheapest :slight_smile: . The towel will hang on the radiator so naturally it will dry too.
And the system will wait, and then when it is cheapest it will dry it then.

Yes that’s right, but as I edited under me I left it because it may come in handy one day and then I won’t have to look for a solution. As I wrote I’m not a programmer and when I find something I rework it as much as I can and if it works it’s OK :slight_smile:

:slight_smile: Generally, when I die it will be a problem anyway, because the family can’t cope with it all, so I leave the documentation and instructions for the electrician anyway, as it will be necessary to disconnect it all :slight_smile:
Well, and sometimes I duplicate the switch ie there is both automatic and manual:)

Well after a lot of testing and suggestions I managed to get the desired functionality working. What I meant was that during the period when it is warm and the heat pump is only in DHW mode (i.e. hot water heating only), the radiator will switch on when either there is ‘electricity’ from the photovoltaics or when there is a cheaper tariff. It depends what occurs first. There was a question of why? The reason is that if I take a shower in the morning and hang my towel on the dryer, nothing happens to it if it stays wet for a while. It also dries naturally on its own.

Here is my heater.

I used a 4 button zigbee switch (battery operated) as the switch. Each button has a state : short press, double press, long press.
TS0044
I used a zigbee socket to switch on, via zigbee2mqtt. Data reading from the heat pump via the module and after MQTT, photovoltaic data reading via integration (interval 15 minutes)

I have created “helpers”
input_boolean.czy_wlaczyc_grzenijk_lazienka_gora - Should I turn on the heater?
input_number.ile_czasu_grzenij - How long the heater should run ?
input_number.minimalny_prog_solar_edgezalaczenia_lazienka_grzejnik - From what photovoltaic threshold should I switch on the heater?
In automation for button “2” at:
short press

type: turn_off
device_id: b814efe99894e92a15d7f72c*
entity_id: 3c9baa4b32049beaecba1e62*
domain: switch

double press

type: turn_off
device_id: b814efe99894e92a15d7f72c8*
entity_id: 3c9baa4b32049beaecba1e62a*
domain: switch

long press

service: input_boolean.turn_on
data: {}
target:
  entity_id: input_boolean.czy_wlaczyc_grzenijk_lazienka_gora

Tariff sensor :
“Workday” integration
configuration.yaml

template:
    
  - sensor:

      
      - name: Taryfa
      
        state: >
          {% set month = now().strftime('%m') | int %}
          {% set hour = now().strftime('%H') | int %}
          {% set isWorkday = states("binary_sensor.workday_sensor") | bool %}
          {% if isWorkday and hour >= 6 and hour < 13 %}
            peak
          {% elif isWorkday and (hour >= 15 and hour < 22) %}
            peak
          {% else %}
            off_peak
          {% endif %}

Automation regarding the switching on of the heater:

alias: Grzejnik łazienka wlącz
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.czy_wlaczyc_grzenijk_lazienka_gora
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: sensor.aquarea_mode
    state: DHW
action:
  - wait_template: |-
      {{ states('sensor.solaredge_current_power') | float(0) >
            states('input_number.minimalny_prog_solar_edgezalaczenia_lazienka_grzejnik')
            | float(0)  or is_state('sensor.taryfa', 'off_peak') }} 
    continue_on_timeout: false
    timeout: "07:00:00"
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.czy_wlaczyc_grzenijk_lazienka_gora
  - type: turn_on
    device_id: b814efe99894e92a15d7f72c*
    entity_id: 3c9baa4b32049beaecba1e62*
    domain: switch
mode: single

Switch off the heater

alias: Wyłącz grzejnik lazienka_gora
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.lazienka_gniazdo_grzejnik
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: "{{ (states('input_number.ile_czasu_grzenij') | float(0)) }}"
      seconds: 0
condition: []
action:
  - type: turn_off
    device_id: b814efe99894e92a15d7f72c*
    entity_id: 3c9baa4b32049beaecba1e62*
    domain: switch
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.czy_wlaczyc_grzenijk_lazienka_gora
mode: single