Trigger automation every minute, unless it has been triggered by something else

I have an automation (executing a python script) that is triggered by an incoming sensor value every 10 seconds. If there are no new sensor values for some reason (network to the sensor down, sensor off/broken), I still want to trigger that automation once every minute (only after there are no new sensor values). How would I do that?

1 Like

Does the triggering change the value of this sensor?

Yes, the automation triggers on the state change. Here is the current setup:

alias: Set heater power level on "Active Power Total" change
description: ""
mode: single
max_exceeded: silent
triggers:
  - entity_id:
      - sensor.active_power_total
    trigger: state
actions:
  - delay: 1
  - action: python_script.set_heater_power_level_on_active_power_change
    data: {}

Do you really need to run this every second.
But you could trigger on it being stale.
Just add another trigger just like the one you have and add the for x seconds.

Not every second, every minute. But yes, I need it to trigger repeatedly. The “for” makes it trigger only once, right?

Here’s a simple way to do it:

alias: Set heater power level on "Active Power Total" change
description: ""
mode: single
max_exceeded: silent
triggers:
  - trigger: homeassistant
    event:   start
actions:
  - repeat:
      while: "{{true}}"
      sequence:
     
        - wait_for_trigger:
          - trigger: state
            entity_id:
              - sensor.active_power_total
          timeout:
            minutes:  1

        - action: python_script.set_heater_power_level_on_active_power_change
          data: {}

        # I'm not sure why this delay is here, but in your version
        # and in this one, if a state change happens during the delay,
        # it won't be caught by this automation.
        - delay: 1

I wouldn’t solve it this way on my system, though. The way I’d probably do it would be to:

  1. Create a timer.
  2. Trigger the automation on the state change, much like you’re doing.
  3. Add a second trigger on the timer firing.
  4. After calling the Python script, I’d reset the timer to 1 minute.

That’s a little more involved, though, because you need to coordinate a new entity (the timer).

You can just add a second trigger and then choose the appropriate path to execute:

alias: Set heater power level on "Active Power Total" change
description: ""
mode: single
max_exceeded: silent
triggers:
  - trigger: state
    entity_id: sensor.active_power_total
    id: sensor
  - trigger: time_pattern
    minutes: /1
    id: time
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: sensor
        sequence:
          - delay: 1
          - action: python_script.set_heater_power_level_on_active_power_change
      - conditions:
          - condition: trigger
            id: time
          - condition: state
            entity_id: sensor.active_power_total
            state: null
            for:
              minutes: 2
        sequence:
          - delay: 1
          - action: python_script.set_heater_power_level_on_active_power_change