Trigger with current state

Those requirements are met by Didgeridrew’s example (with or without the Time Trigger and second State Condition). The following automation detects when p1 is turned on and then turns on p2 but only if it’s currently within the desired time period.

alias: example 
trigger:
  - platform: state
    entity_id: switch.p1
    to: 'on'
condition:
  - condition: time
    after: "14:30"
    before: "21:30"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.p2

However you said Didgeridrew’s example didn’t work for you so that implies the requirements you have posted don’t accurately describe what you really want.

You will need to explain what you tried.

Did the state of p1 change from ‘off’ to ‘on’? One of the triggers has to fire for the actions to be executed.

If you expect it to check the state of p1 and the time when Home Assistant restarts or the automations reload, those also need to be added as triggers. I have edited the automation posted above to include those triggers as well.

Again, I understand, and that code effectively works when the state changes aka turn on.
But you have to remember the state of my p1 doesn’t change: has changed in the past (to on).
I want to trigger if the detected device state is on, not if some state change it’s detected (from whatever-to-whatever state)…

Nope, man… I can’t make the state changes during that hours, it changed previously and it could remain as it is during that whole time range.

I just want to detect which is the state, not some state changing.
Is on? Turn it on. Is off? Don’t turn it 'on.

As was explained previously, and more than once, that doesn’t qualify as a trigger.

Your understanding of what constitutes a trigger still doesn’t conform with Home Assistant’s definition of the term. I strongly recommend you review the Trigger documentation to understand how triggers work and what kind are available in Home Assistant.

Let’s assume the plug was turned on Tuesday last week, so it’s now been on for more than 7 days. Today you will create an automation that detects if the plug is on, and if the time is between 14:30 and 21:30, then it will turn on the LED strip.

What kind of trigger will you use to trigger the automation?

  1. Will you use a Time Trigger to trigger exactly at 14:30 and check if the plug is on?
  2. Will you use a Time Pattern Trigger to trigger every minute to check if the plug is on and the current time is between 14:30 and 21:30?

If it’s none of the mentioned trigger types, review the list of available triggers and choose one that fits your needs. If you believe none of them are suitable then the scenario you have in mind simply doesn’t qualify as a trigger.

Ok, no problem, I was using the wrong terms. I’ll read the doc carefully.

Correct

I.e. the state condition (Conditions - Home Assistant) would be more than enough, but it’s not available as a trigger (as I can see).

No, not exactly, I need a something like time condition (Conditions - Home Assistant), which uses a time range and doesn’t trigger only at that hour.

As we’ve seen, this method isn’t recommended: every minute of the entire day would trigger to check (huge amount of written data - I suspect)

No problem, I suspected, looking at the list of triggers, something like that wasn’t directly achievable, but I won’t give up. Something basilar like that must be a joke for Home Assistant.

I was thinking about scripting, using a time check within an if-then (Script Syntax - Home Assistant), but I ignore if that is possibile

Conditions are checked after an automation has executed its trigger (and it changed from false to true).

A State Condition compares an entity’s state value to a reference value. It makes a true/false decision. When does it make this decision? After the automation’s trigger has executed. Why? Because a steady-state value isn’t an event (unless time is involved) and triggers are event-based (i.e. they monitor and detect changes).

A State Trigger detects when an entity’s state value changes to a new value. It optionally compares the new value to a reference value (to, from) and use a time constraint (for).

A Time Condition compares the current time to a reference time (or time range). It makes a true/false decision after the automation’s trigger.

A Time Trigger detects when the current time changes and matches a reference time (or times).

A script is functionally equivalent to an automation without a trigger. A script does nothing until you execute it with a service call.


EDIT

I believe Didgeridrew’s example fulfills your described requirements. It turns on p2 but only if p1 is on and the time is within a desired range. It achieves this efficiently, using a combination of triggers and conditions.

You claimed it “doesn’t work” but, respectfully, I suspect you don’t understand how it works. You said “I can’t make the state changes during that hours”. P1 isn’t obligated to make a state-change during the time range. If p1 is on before the time range, the moment the current time enters the time range, the automation will trigger, confirm p1 is on and turn on p2. That’s what you had requested; turn on p2 during the time range but only if p1 is on.

As soon as I’ve read his solution, I myself thought that it would work, but then I tried and it didn’t.
Maybe I did something wrong during the test. As soon as possible I will retry

Yeah, it should work exactly like that. I will retry and select that as the solution

What was the testing procedure?

1 Like

Ok, everything now works! :champagne:
platform here (line 15) should be substitute by condition. That was just a syntax error.
When I changed it, leds immediately turned on (within the time range).

Thank you all @Didgeridrew, @123 :handshake: