Trigger with current state

Hi! I’d like to know how to trigger an action in at least one this scenario :point_down:

  • if a device is on a certain state within a given time range
  • if a device has been on a certain state for some time within a given time range (optional)

I.E.
(d=device, t=trigger, c=state checking automation, r=time range specified in c):

  1. c starts and stays on
  2. time passes, r is reached (we are now between hour_x and hour_y)
  3. d is on on state (optional: has been such for x time)?
  4. activate t

NB:

  • Even if d was turned on before r has reached, trigger must function the same
  • Trigger should be based only on current device state, not some (device) state changes

Triggers in Home Assistant are based on events, something has to happen like a state change or the passage of time. It isn’t recommended for most things, but you can use a Time Pattern trigger to regularly poll the state of an entity.

trigger:
    - platform: time_pattern
      minutes: "/1"
condition:
  - alias: "switch has been on for at least 5 minutes"
    condition: state
    entity_id: switch.lightbulb
    state: 'on'
    for: 
      minutes: 5
action:
  - service: switch.turn_off
    entity_id: switch.lightbulb

Keep in mind that this is a very inefficient way to design an automation. The automation above would trigger 1440 times a day. Every time it triggers, it has to do a little calculation to see if the switch has been on for at least 5 minutes. Even if someone turned the light on immediately every time it was turned off, 1152 of those calculations would be wasteful.

I’m okay with the triggers policy and with your solution.

So what would be the best way to trigger with a current state without using time_pattern (as you correctly said, inefficient)?

It wouldn’t be possible to do something like:

condition:
  - platform: time
    before: "hours_x"
    after: "hours_y"
  - alias: "switch has been on for at least 5 minutes"
    condition: state
    entity_id: switch.lightbulb
    state: 'on'
action:
  - service: switch.turn_off
    entity_id: switch.lightbulb

As you can see, without using time pattern or activation duration…?

My goal is to trigger the action just using the current state of the device. That would be my trigger event.
If the detected state it’s on, do this, if it’s off, do that.

Adding activation through “device has been active for x time” or “between hours x and hours y” would be a plus, not necessary.

Is this possible?

A stable state is not an event. Can you explain why a state change (with or without a for:) is not compatible with your needs?

Cause some states don’t change (even considering a strange off-to-off changing) in hours, and some never change at all.

But I want my device to turn off if the detected state it is what I choose (obviusly, when the checking automation is on), and wait for a state to change it’s simply a waste of time while knowing that state isn’t going to change.

Just to clarify: I workarounded the whole thing just playing with automation’s activation time, but I think something like that would be useful.

Give the chance to trigger based only the state of the device, not using activation time nor time patterns controls

That’s a State Trigger with a for option.

trigger:
  - platform: state
    entity_id: switch.whatever
    to: 'on'
    for:
      minutes: 10

What’s your reason for these constraints?

How do you propose to determine an entity’s state given that you disallow listening for its state-changes and periodically checking its state? Effectively your constraints are “Don’t listen to it and don’t look at it.”

That’s not a “trigger event”. An unchanging state doesn’t qualify as a trigger.

What does qualify is an unchanging state for a given period of time. That’s implemented by the State Trigger example I posted and Didgeridrew’s Time Pattern example.

As explained, if the entity’s state remains unchanged, that is unremarkable and doesn’t serve as a trigger. However, if the state remains unchanged for a certain period of time, it might represent a defective device (i.e. it’s not being updated regularly). That’s sufficient to serve as a trigger (and is supported by the State Trigger).

You may wish to review the definition of a Trigger.

Are you regularly enabling and disabling automations? If so, that is an unrecommended design pattern. The automation’s logic should determine if and when it should perform any actions. Disabling automations can be necessary when debugging or makng system changes. If you use automations to regularly enable or disable other automations instead of designing your automation’s logic properly, you lose a degree of the manual control you may need when fixing issues.

1 Like

Ok, perhaps that will do the trick.
With that I could set minutes to an irrilevant value (like 1 sec) and activate the trigger almost like it’d be what I search for.

Now the only doubt is when the for count starts.
For example:

  • automation within this trigger starts at 14:30.
  • at the same time or before (time<=14:30) suppose the device is on on state.

Will the for within this automation count also earlier activated time cicles?
Will, then, the trigger turn off the device when the 10 minutes (that started before the automation started) of on state shall be reached?

Definetely not regurarly. I have only one automation that checks if another automation isn’t activated.
I was already questioning myself about the rightness of that modus operandi, but I did that exclusively cause it was the quickest way to get what I wanted to, and I will avoid this in future😂

It starts counting down the value specified by the for option (like 10 minutes) from the moment the entity’s state changes to on.

In the example I posted, if the entity’s state changes from off to on at 14:30:00 and remains unchanged in that state for at least 10 consecutive minutes, the State Trigger will trigger at 14:40:00.

It starts counting at the moment the entity’s value changes from any value (off, unavailable, unknown) to on. That’s what the to: 'on' does in the State Trigger’s configuration and the for specifies the time period that the state must remain unchanged in order to perform a trigger.

This technique is commonly used to automatically turn off an entity that has been turned on and left on for a specified period of time.

But here’s my problem.

Suppose we are between 14:30 and 21:30.
In this time interval I want to turn on a led strip (if that’d be a problem, say I don’t even care about its state, I just want to send aprioristically a “turn on” message) if another plug is already on (turned on a while ago, I ignore how much).
The other plug might be turned on 10 minutes ago or 4 hours ago, so (to my orrendous knowledge) I can’t use a device trigger.

As I said, for clause it’s not important for my scope, I just want Home Ass to turn on my device if we are in that time range and If another plugged was turned on a while ago yet

Just to clarify what you want:

You want to turn on the LED strip but only if some other plug is already on?

Yes, but only between hour x and hour y

What will actually turn on the LED strip between hour x and hour y?

Is it when a user turns on the LED strip or the action within some other automation or script or what?

Strip is on the plug “p2”, call the other plug “p1”.

Currently an automation turns p2 on after 15 secs has passed since p1 has turned on.
This happens only if we are between hour x and y (time range).

But that’s not fair, cause if I turn p1 on while I’m out the chosen time range, p2 won’t turn on automatically when I enter that interval (while p1 obviously is yet on)

Set a second trigger for the start of your time period, and set conditions that check for both the switch state and the time.

trigger:
  - platform: time
    at: "14:30"
  - platform: state
    entity_id: switch.p1
    to: 'on'
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - condition: time
    after: "14:30"
    before: "21:30"
  - condition: state
    entity_id: switch.p1
    state: 'on'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.p2

If you want, you could also add a trigger for the end of your time period to turn off p2, but that requires a Choose or If/Then action:

On & Off version
trigger:
  - platform: time
    at: "14:30"
  - platform: state
    entity_id: switch.p1
  - platform: time
    at: "21:31"
condition: []
action:
  - choose:
    - conditions:
      - condition: time
        after: "14:30"
        before: "21:30"
      - condition: state
        entity_id: switch.p1
        state: 'on'
      sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.p2
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.p2

EDIT: Added triggers to the automation to cover Restart and Automation Reload.

If think this will work quite well. As soon as I get home, I’ll try it out

Sadly, it doesn’t work

Based on my interpretation of your replies, you want to turn on the LED strip (switch p2), within a time range (14:30 - 21:30), but only if another device (switch p1) is already on.

Do you want the automation to turn on p2 at 14:30 or to simply detect when p2 is turned on by anyone (a user, another automation, etc) after 14:30 (but before 21:30)?

Yeah, you’re right.
The second one: simply detect if p1 is on and if is on, turn on p2.
This within the time range