Action based on state attribute not changing

Hey guys,

I have an EZVIZ camera, integration is set up. but the pir atribute is not working yet.
My outside light is triggered based on a door sensor at the back.
I can get the “Last alarm triggered:” value.
I want to turn off my light if the value from Last alarm triggered is unchanged for “5 seconds” as a mather of testing.

according the the home assistant docs. if you give no “from/to” value, but only a set time. this shoud check if the state doesn’t change for the 5 seconds and trigger.

i tried a real simple version of it. but get no reaction.

alias: test
description: ''
trigger:
  - platform: state
    entity_id: camera.husky_air_d53127471
    attribute: Last alarm triggered
    for: '00:00:05'
condition: []
action:
  - type: turn_on
    device_id: 5ef5140c47f89176e18a195fad4658ff
    entity_id: light.licht_keukeneiland
    domain: light
mode: single

Im probley missing something ?
sorry for my typo’s ! and thanks for being an amazing community.

I think the attribute format was not correct. Try this please

alias: test
description: ''
trigger:
  - platform: state
    entity_id: camera.husky_air_d53127471
    attribute: last_alarm_triggered
    for: '00:00:05'
condition: []
action:
  - type: turn_on
    device_id: 5ef5140c47f89176e18a195fad4658ff
    entity_id: light.licht_keukeneiland
    domain: light
mode: single
1 Like

@sheminasalam is probably correct.

You can get the correct name of the attribute when you look at the entity under Developer Tools -> States

1 Like

Has no effect, i already tried this.
In developer tools i can see it update but automation Doesnt work.

This is where i got it. Regardless adding _ has 0 effect.

It looks like the action part could have had another issue. please try this. Also make sure the attribute is correct

alias: test
description: ''
trigger:
  - platform: state
    entity_id: camera.husky_air_d53127471
    attribute: last_alarm_triggered
    for: '00:00:05'
condition: []
action:
  - service: light.turn_on
    data: {}
    entity_id: light.licht_keukeneiland
mode: single

Works half now !
Apperently this works if state changes. Then 5 sec and action. I wanted no state change 5 sec action.

Ty for the help btw !

happy to help. I feel its basically the same…right. If a state holds for 5sec, it means the there is no state change for 5s. Another method is to use a template to trigger the node but it again is basically the same thing.

In this case no, caus i want my lights to go out if there is no state change. Every motion makes a New last alarm. So still doesnt work for me. I did Lear something just not what i want.

Don’t you want turn_off in the action section?

Assuming it triggers, here’s the logic I see:

  1. last_alarm_triggered triggers the automation, waiting for 5 seconds
  2. After 5 sec, the action section executes, turning the light on with turn_on command.

Yes, turn on is easyer for me to see cause in testen light is always off. I just want the action to happen if state remains the same. So if no last alarm trigger has happend for 5 sec. The action ( on or off ) has to happen.

See above comment :slight_smile:

Got it! That makes a lot of sense!!

I really think you’ve got it, then. The idea of the for statement is to make sure the state stays locked in for xx seconds before executing the actions. If it does stay locked in, then it triggers. It seems like that’s what happened. For example:

  1. State changes - internal 5 sec timer starts in HA (unfortunately I don’t know how to watch this)
  2. State changes again after 4 sec - internal HA timer restarts at 5 sec and actions are not executed
  3. This keeps happening until it does take 5 sec, then actions execute

Does that make sense?

I understand, but if i f i put the light off . And no state change the light stays off, nothing happens. If it changes the last state (because allarm trigger) and then there is no change for 5 sec it triggers. But i want it to trigger when nothing happens. If there is 0 activity and nothinging happens i want my action.

Sorry fory english but its hard to explain.

In theory it shoud trigger every x seconde because nothing happens. But only triggers when it gets detection.

Here is my understanding of how this trigger works.

  - platform: state
    entity_id: camera.husky_air_d53127471
    attribute: last_alarm_triggered
    for: '00:00:05'
  • If the value of last_alarm_triggered remains unchanged for a period of 5 seconds, it triggers the automation.

  • It won’t trigger again until the value changes and then remains unchanged for a period of 5 seconds.

Based on this, you want it to trigger when nothing happens. That’s not really the way triggers work - some event has to trigger it. You could always add a time trigger to trigger every XX minutes/hours/seconds. We tend to use that as a last resort because triggers should be based on something happening (alarm activated, light turned on, etc). (@123 has an excellent philosophy on that which makes sense to me). But I admit I have a few of them because I didn’t have any other well to trigger.

Is this the logic you are after?

  1. Nothing has happened for a while - the light is on for some reason (?). Or maybe it’s off
  2. The automation triggers every xx unit of time (minutes, hours, seconds) and if the alarm hasn’t triggered recently, then turn the light off.

The only way I can think of to do that is to use a time trigger ( Automation Trigger - Home Assistant (home-assistant.io))

@123 states it really well - to trigger, something has to change.

I thought (originally) you wanted to turn the light when the alarm triggers (assuming that is in a separate automation) and then this automation turns the light off if the alarm has been off for a period of time. But now I’m thinking I misunderstand your original request.

you got it just right !

What other attributes does this camera have? Post a screenshot of its attributes.

If it has an attribute indicating the last time there was an alarm, is there an attribute that indicates the alarm’s current state (on/off)?

For example, if it has an attribute called alarm, that indicates on or off, you could do this to indicate when it’s been off for 5 seconds (equivalent to 5 seconds of last_alarm_triggered):

  - platform: state
    entity_id: camera.husky_air_d53127471
    attribute: alarm
    to: 'off'
    for: '00:00:05'

Pir doesnt work!
Ty for the help.