Best way to trigger on update of same state

I want to trigger a timer whenever a sensor reports 'on', even if it was not 'off' before. The last_changed attribute is not changed. What is the best way to handle this?

So what state was it before it changed to on?

wouldn’t it be better to use a timer to check the status?

In fact I want to trigger a timer - and also reset it. But the state goes from on to on.

I guess I just have to trigger every minute or so. I wanted to avoid that.

So the state does not change but you want to use it as a trigger?

What exactly is the problem you are trying to solve, not how you want to do it, the actual problem. There may be a better way.

Trying to avoid an x-y problem here.

I check environmental states using multiple sensors. If a combination of these sensors report ‘on’ (using a template) I assume it should be on for at least 5 minutes. So I set an input_boolean to on. Only after 5 minutes if the sensor reports off, it is possible to become off again. That is why I need the timer to reset, even if there is no change.

home assistant by default supresses on → on state changes. Also, input_booleans inherently suppress on → on state changes. I.e. calling the service homeassistant.turn_on with an input_boolean does not actually change the state.

So please explain what your end goal is because your current explanations don’t really tell us much.

Can you use a /5 Time Pattern trigger (Automation Trigger Variables - Home Assistant) which would fire every 5 minutes?

Alternatively, for quicker response, could trigger based on state change to ‘off’ of your template using a template trigger (as well as when it goes back on).

Agree with Petro to an extent - more info on your specific use-case / problem you’re trying to solve might help users help to get you to your desired solution more quickly/easily.

The automation is a bit complex. But in the end I have a template and an input_boolean.

If the template goes off-on, input_boolean will go on.
If the template goes on-on, input_boolean will stay on.
If the template goes off, input_boolean will stay on for 5 minutes.

Now that I am writing this, it would probably make sense to use the timer on the off-state. :slight_smile:

If this is a template binary sensor just use delay_off: 300.

If the template goes off-on, input_boolean will go on.
If the template goes on-on, input_boolean will stay on.
If the template goes off, input_boolean will stay on for 5 minutes.

uh, just add a delay off to the template and get rid of the input_boolean…

1 Like

The sensor is a shell_command that returns on or off.

I can’t trigger on off because I need the timer for the set off conditions.

Create a template_binary sensor instead then.

delay off

You could break into two automations.

  1. Triggers every 5 minutes based on Time Pattern, with a condition of Template = OFF and sets the input_boolean to OFF (false)
  2. Triggers when Input Boolean changes to OFF, has a delay of 5 minutes as part of the actions and then sets input boolean to ON

You could also use #1 above and include the starting of a 5 minute timer as part of the actions and then #2 would fire when timer is finished and reset the input boolean (this [based on latest HA update to 2022.4.x] would survive a restart)

Link to DELAY_OFF: Template - Home Assistant

If it doesn’t do what you want “delay_off = The amount of time the template state must be not met before this sensor will switch to off . This can also be a template.” Then can try some iteration of my suggestions above.

Thank you all. I think I got it now.

I have created a template_binary_sensor.

This sensor will go on, when the template returns on.
If it returns off, it will wait for 300 s unless it will got on again in the meantime.

  - binary_sensor:
    - name: "test"
      unique_id: "test_23"
      unit_of_measurement: ""
      delay_off: 300
      state: >
        {{ ... }}

Is that correct?

      delay_off: 
        minutes: 5

or

delay_off: "00:05:00"

or

delay_off:
  seconds: 300

etc.

1 Like

I have a very similar use case. I am using a Tuya scene switch with returns a click, double or long value based on how the switch is clicked.
Trouble is that the state of the sensor doesn’t update when the previous state and the new state are the same. e.g. If Button 1 was clicked, the sensor reports click. If after a few minutes Button 1 was clicked again, the state of the sensor still stays as click.
So I have no idea how to trigger an event when a button is clicked because I cannot use the state change as my event.

Usually a button/switch used for scenes doesn’t involve a state change, but HA still sees the “scene” event on its event bus. How to capture this event in an automation is usually technology/platform/integration dependent and not so easy. One suggestion would be to do a search in HA community for a Blueprint on your specific device. Here is an example and here is another example.

Trouble seems to be that absolutely no event is getting generated when the button is clicked and then clicked again. The State of the sensor doesn’t change and HA doesn’t raise the state_changed event as it sees no change in the state. The only time an event is seen on the event bus is when the button is pressed differently e.g. click vs long press