#Simple automation is... not.

1 messages · Page 1 of 1 (latest)

untold drift
#

I've tried to do this several ways now. I have a button that controls an automation -- and it will start the automation, to a point. But then if I add anything to the automation it won't work. Here's what I'm trying to do:
I want to press a button. It will turn on a light, leave it on for a specific time, then turn that light off. Then, at a specific time (say 10pm) I want the same light to turn back on. I know this is possible, but so far nothing I've tried works. I can get a light to turn on then off using either scripts or automations. But I can't get the thing to do the full cycle. Here's the latest in a string of automations I've tried:

alias: Testlights2
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.testlights
conditions: []
actions:
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.game_lights
  - delay: "00:05:00"
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.game_lights
  - wait_for_trigger:
      - trigger: time
        at: "15:25:00"
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.game_lights
mode: single

Any ideas why this doesn't work?

lost quarry
lost quarry
untold drift
#

I put everything in a script and that works

#

Except I haven't hooked it up to the button yet

lost quarry
#
alias: my test automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.my_test_button
    id: button-pressed
  - trigger: time
    at: "15:25:00"
    id: specific-time
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - button-pressed
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.bedroom_ceiling
          - delay:
              minutes: 5
          - action: light.turn_off
            target:
              entity_id: light.bedroom_ceiling
      - conditions:
          - condition: trigger
            id:
              - specific-time
        sequence: 
          - action: light.turn_on
            target:
              entity_id: light.bedroom_ceiling
mode: single

The way it works is that:

  • when you press the button, the button-pressed trigger fires. this trigger has its own id. The actions part will use the choose and select the path that is a first pass. In this case it is the triggered by button-pressed. Light goes on, stays on 5 seconds, goes off.
  • when it is specific time, it only goes on, as in your example
#

In your example, automation didn't complete until "wait_for_trigger" happened, which may last for a very long time.

untold drift
lost quarry
#

yes the code runs sequentially, but you had your "wait for trigger" for a specific time of day, let's say evening

#

if you press teh button in the morning, time to reach the even is like 12 hours

#

so code will wait 12 hours for time to be as set in your automation

untold drift
#

So I guess my question is, with your code, will those lights always trigger at 3:25?

#

Ah well... that code didn't work either. The lights never came on at all.

lost quarry
#

how do you test the 3:25 clock?

#

unless you are in India, your minutes are now 04

untold drift
#

I'm in the US and its a bit after 4pm

lost quarry
#

so set the time 2 minutes after your current time

#

and save automation

#

and check the traces of the automations few minutes later

#

the traces is super powerful tool, it shows you the way autoamtion was executed

untold drift
#

So if that automation is active, no matter if I press the button or not, it will turn on the lights at whatever time I told it to. Which is not what I want. I ONLY want those lights to turn on IF I've pressed the button.

lost quarry
#

ok so you want the automation to only start when button is pressed, and then they are on for 5 minutes, and after that turn it again at specific time

#

when do you turn them off finally?

untold drift
#

I'll turn them off manually since I'm never sure when I'll be getting home in those instances.

lost quarry
#

a ok, well I'd do it differently, it would still be a double trigger as before, but if the trigger was the button press, I'd also set the helper input to true

#

and when the trigger is for time, it would check the input helper with "if" in the actions and only then turn on lights. And finally clear the input helper in the time trigger part of the automation

#

because your automation with "wait for trigger" is prone to error. If home assistant restarts while wiating for trigger, automation will stop and light won't go on afterwards

untold drift
#

That seems like it would work. And if I were doing it in actual code, like c#, then I could do it pretty easily. In this... not so much

lost quarry
#

the helpers are considered "global variables" in home assitant 🙂

#

this one is your friend

untold drift
#

The helper I set up is an input BUTTON. Maybe it needs to be input boolean instead?

lost quarry
#

I have this for example for my weather announcement. I want it every morning when motion detects me in the kitchen, but only once a day, so trigger is a motiond etection and I check if the toggle is off. If it is off, alexa announces, if it is on, I don't do anything.

And then another trigger is at 3 AM, which resets the toggle for the morning routing to restart

lost quarry
#

input button is what calls a function in C#, input boolean is what checks inside the function what part of function will execute

untold drift
lost quarry
#
alias: my test automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_button.my_test_button
    id: button-pressed
  - trigger: time
    at: "15:25:00"
    id: specific-time
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - button-pressed
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.bedroom_ceiling
          - delay:
              minutes: 5
          - action: light.turn_off
            target:
              entity_id: light.bedroom_ceiling
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.your_input_boolean_helper
      - conditions:
          - condition: trigger
            id:
              - specific-time
        sequence:
          - if:
              - condition: state
                entity_id: input_boolean.your_input_boolean_helper
                state: "on"
            then:
              - action: light.turn_on
                target:
                  entity_id: light.bedroom_ceiling
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.your_input_boolean_helper
mode: single
#

something like that I guess

#

so you turn it on if the button is pressed + light goes on for few minutes.
And at specific time trigger, you check the input boolean, if on, turn on lights, if no, don't do anything. At the end, clear the boolean, for next day (reset the status)

untold drift
#

Looks solid.

lost quarry
#

you see, HA is as simple as C#, just context is a bit different 😉

untold drift
#

Well... ya gotta kinda know what means what. I don't do much with yaml. Seems a bit like python.

lost quarry
#

What I gave you was made in UI to be good for you to copy/paste and understand, with good indent

untold drift
#

Yeah, it looks good, thanks. I'll take and modify it to match my vars

#

I'm using the toggle so the state for that is a bit different.

    entity_id:
      - input_boolean.game_lights_helper
    from: "off"
    to: "on"```
lost quarry
#

so you want to trigger it by transitioning the toggle and then keep the state of the toggle until time comes

#

that can work indeed

#

manage the case when you toggle the case after the time tirgger

untold drift
#

Yeah. That way I know it's on and running.

lost quarry
#

who is the disabling it?

untold drift
#

Well, I'd guess that the switch can be toggeled off once the lights are turned on the second time.

lost quarry
#

let's say your second time is at 8pm

#

you toggle the switch at 9pm

untold drift
#

That'd work. As long as the lights stay on.

lost quarry
#

the lights will go on second time next day at 8pm, which I don't know if you want

untold drift
#

No, that I don't want.

lost quarry
#

then you can do another trigger at like 3 AM, which will only clear the input toggle to be off

#

regardless of when you toggled manually your state from off to on

untold drift
#

That'd work.

lost quarry
#

This is my alexa state which does what you'd want eventually:

alias: "Alexa: Notify weather state when first entering living room"
description: >-
  Alexa will report weather forecast for my location once I enter the living
  room for a first time in a day. It must be after 05:00 AM
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.kitchen_motion
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
    id: motion-detected
  - trigger: time
    at: "03:00:00"
    id: time-to-reset
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - motion-detected
          - condition: state
            entity_id: input_boolean.alexa_asked_to_report_weather_status_for_the_day
            state: "off"
        sequence:
          - action: media_player.play_media
            target:
              entity_id: media_player.echo_spot
            data:
              media_content_type: custom
              media_content_id: >-
                Please provide me with the weather forecast for today in the
                city <my city>. Please indicate temperature range in
                celsius
          - action: input_boolean.toggle
            target:
              entity_id: input_boolean.alexa_asked_to_report_weather_status_for_the_day
      - conditions:
          - condition: trigger
            id:
              - time-to-reset
        sequence:
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.alexa_asked_to_report_weather_status_for_the_day
mode: single
untold drift
#

This one looks like it might work

description: ""
triggers:
  - trigger: time
    at: "16:05:00"
    id: specific-time
  - trigger: state
    entity_id:
      - input_boolean.game_lights_helper
    from: "off"
    to: "on"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - button-pressed
        sequence:
          - action: light.turn_on
            data: {}
            target:
              entity_id: light.game_lights
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - action: light.turn_off
            data: {}
            target:
              entity_id: light.game_lights
      - conditions:
          - condition: trigger
            id:
              - specific-time
          - condition: state
            entity_id: input_boolean.game_lights_helper
            state: "on"
        sequence:
          - action: light.turn_on
            data: {}
            target:
              entity_id: light.game_lights
mode: single```
lost quarry
#

yes but it doesn't solve the problem of switch being toggled after 16:05

untold drift
#

True. But if I add another trigger for say, 3am, then I can check and see if the toggle is true. If it is, set it to false.

lost quarry
#

you just set to false, no need to check.

untold drift
#

Heck, I could set the thing to false after the lights are turned on -- provided this isn't some sort of await set up when I don't know the order that things will run in.

lost quarry
#

well you can't do that as if you toggle the thing after time

#

time won't turn it off and next time will be next day

#

it will be fine if next day you press the button again because that's what you want

#

but if you don't press it, light will still go on

untold drift
#

Wow. All that programming and it still doesn't work. Apparently once a condition has been completed that's it. The automation ends.

#

None of the other branches are run.

untold drift
#

So this version works. Gotta use a run in sequence and it solves a lot of problems:

description: ""
triggers:
  - trigger: time
    at: "17:07:00"
    id: Time-On
  - trigger: state
    entity_id:
      - input_boolean.game_lights_helper
    from: "off"
    to: "on"
  - trigger: time
    at: "17:09:00"
    id: turn_off_toggle
conditions: []
actions:
  - sequence:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.game_lights
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.game_lights
      - wait_for_trigger:
          - trigger: time
            at: "22:00:00"
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.game_lights
      - wait_for_trigger:
          - trigger: time
            at: "23:59:00"
      - action: input_boolean.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.game_lights_helper
mode: single