Automation - actions "while" every minute

Hello all,

I cannot seem to be getting my automation to work. Any help would be appreciated.

The context:
I want a sensor to be updated every minute, but ONLY when a switch is ON (and after 3 minutes of it being switched on). For those who are curious, I have a temperature probe within a swimming pool pipe and want an accurate reading on the temperature, i.e. only when the filtration is on.

I don’t want continuous updates, only every minutes, sadly my “while” condition is failing so far :
{The notifications are used as a test : my phone is getting spammed like crazy]

alias: Pool Temperature
description: Determines the pool temperature once filtration is ON.
trigger:
  - platform: device
    type: turned_on
    device_id: 6fd153ce7bda119b15030483841a7b77
    entity_id: a7c4800a3935b6c2ef5a73e73815f6ad
    domain: switch
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - repeat:
      while:
        - condition: device
          type: is_on
          device_id: 6fd153ce7bda119b15030483841a7b77
          entity_id: d7d1a58545a128d23cbe2f0d1694f1d1
          domain: switch
          for:
            hours: 0
            minutes: 0
            seconds: 30
      sequence:
#        for:
#          hours: 0
#          minutes: 0
#          seconds: 30        
        - service: notify.mobile_app_phone
          data:
            message: Pool Temperature is at

        - service: input_number.set_value
          metadata: {}
          data:
            value: "{{ states('sensor.pool_air_temperature') | float}}"
          target:
            entity_id: input_number.pool_temperature
    enabled: true
mode: single

Latest attempt is commented out and has failed miserably, YAML won’t take it.

Any help much appreciated!

Many thanks

Well I may be able to respond to my own question ; I suspect my logic was wrong and that I simply need to add some delay.

alias: Pool Temperature
description: Determines the pool temperature once filtration is ON.
trigger:
  - platform: device
    type: turned_on
    device_id: 6fd153ce7bda119b15030483841a7b77
    entity_id: a7c4800a3935b6c2ef5a73e73815f6ad
    domain: switch
condition: []
action:
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - repeat:
      while:
        - condition: device
          type: is_on
          device_id: 6fd153ce7bda119b15030483841a7b77
          entity_id: d7d1a58545a128d23cbe2f0d1694f1d1
          domain: switch
      sequence:
        - service: notify.mobile_app_pixel_7
          data:
            message: Pool Temperature is at
        - service: input_number.set_value
          metadata: {}
          data:
            value: "{{ states('sensor.pool_air_temperature') | float}}"
          target:
            entity_id: input_number.pool_temperature
        - delay:
            minutes: 1
    enabled: true
mode: single

The trigger would be fine as is but the initial delay (3min) would have to be the first “action”.
Following that, the second delay (= every minute) would have to be in the “while” sequence.

If there is a more elegant method I’m all ears, it’s still early days on HA for me, but it seems that the above code works.

Thanks for reading!

…is to reduce the time the automation takes to run: trigger off a change, check if OK to continue, do something and finish. Minimise any loops or logic flows in the action.

The sensor.pool_air_temperature is probably only sending updates every minute or so anyway. I’m not sure what the purpose of your input_number is.

Try this, which triggers any time there’s a change to the temperature, but only continues to the action if:

  • the switch is on
  • the automation has either never run or not run in the last 50 seconds
alias: Pool Temperature
description: Determines the pool temperature once filtration is ON.
trigger:
  - platform: state
    entity_id: sensor.pool_air_temperature
condition:
  - condition: state
    entity_id: switch.[THE ENTITY ID OF YOUR SWITCH]
    state: 'on'
  - "{{ state_attr('automation.pool_temperature','last_triggered') is none or (now()-state_attr('automation.pool_temperature','last_triggered')).total_seconds() > 50 }}"
action:
  - service: notify.mobile_app_pixel_7
    data:
      message: "Pool Temperature is at {{ states('sensor.pool_air_temperature') }}°C"

Double-check the entity ID of the automation, used twice in the second condition.

1 Like

Thank you, this is much better indeed. One thing I can’t get to work is the 50 second delay though.
While the switch is ON, whenever the temperature changes, I receive a notification : I can potentially receive as many notifications as I have temperature changes within the 50 second time frame. Not sure what I’m doing wrong here (adapted code):

alias: Pool Temperature (NEW)
description: Determines the pool temperature once filtration is ON.
trigger:
  - platform: state
    entity_id: sensor.pool_air_temperature
condition:
  - condition: state
    entity_id: switch.pool_1
    state: "on"
  - condition: template
    value_template: >-
      {{ state_attr('automation.pool_temperature','last_triggered') is none or
      (now()-state_attr('automation.pool_temperature','last_triggered')).total_seconds()
      > 50 }}
action:
  - service: notify.mobile_app_pixel_7
    data:
      message: Pool Temperature is at {{ states('sensor.pool_air_temperature') }}°C

Apart from that the logic is indeed much better, thank you for that.

As I said, check the entity ID of the automation. If it isn’t automation.pool_temperature, update it as appropriate in the template.

Check this in the developer tools. Your automation has new in the name, so this might not be the automation that sent you the notification. That would explain why it keeps notifying.

Thank you, indeed, I’ve missing the “new” part. The older automation still existing it did not generate an error…

Basically this is my (likely clumsy) way of updating a “dummy” sensor that displays the “real” (or at least, last measured) temperature of the pool. I created a Helper.

Corrected code is now:

alias: Pool Temperature (NEW)
description: Determines the pool temperature once filtration is ON.
trigger:
  - platform: state
    entity_id: sensor.pool_air_temperature
condition:
  - condition: state
    entity_id: switch.pool_1
    state: "on"
  - condition: template
    value_template: >-
      {{ state_attr('automation.pool_temperature_new','last_triggered') is none
      or
      (now()-state_attr('automation.pool_temperature_new','last_triggered')).total_seconds()
      > 50 }}
action:
  - service: notify.mobile_app_pixel_7
    data:
      message: Pool Temperature is at {{ states('sensor.pool_air_temperature') }}°C
  - service: input_number.set_value
    metadata: {}
    data:
      value: "{{ states('sensor.pool_air_temperature') | float}}"
    target:
      entity_id: input_number.pool_temperature

Good news is this is working superbly well, thank you. Bad news is that the pool is still only at ~7°C, possibly ambitious as of now!

If there’s a neater way to update a “dummy” sensor, I’m also keen to hear about it.

@Edwin_D Thank you, this is a good practice I need to learn and your intuition was spot on.

Depends how you want it to behave when the switch is off. This will be unavailable when the switch is off:

template:
  - sensor:
      - name: Pool temperature if filtering
        state: "{{ states('sensor.pool_air_temperature') }}"
        unit_of_measurement: "°C"
        device_class: temperature
        availability: "{{ bool(states('switch.pool_1')) }}"

Seems better than displaying an incorrect temperature, if you don’t want to just always display the real temperature.

Thank you again, I’m going to read up on this and do some testing, this is interesting for sure.

1 Like