Presence Simulation - Is my YAML Code idiomatic?

Hi all,

this evening I sat around 4 hours on creating the following code.

I wanted to build a presence simulation that plays several scenes once after another.

  • When the input_boolean for presence simulation (input_boolean.anwesenheitssimulation) is “activated”, the automation shall start (-> anwesenheitssimulation is the german word for presence simulation)
  • When input_boolean.anwesenheitssimulation is “deactivated”, a final scene shall be activated ending the process

This automation is started when the bool is activated:

alias: AnwesenheitssimulationAN
description: "2024-10-20"
triggers:
  - entity_id: input_boolean.anwesenheitssimulation
    to: "on"
    trigger: state
  - entity_id: input_boolean.anwesenheitssimulation
    to: "off"
    trigger: state
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.anwesenheitssimulation
            state: "on"
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: input_boolean.anwesenheitssimulation
                  state: "on"
              sequence:
                - entity_id: scene.no_one
                  action: scene.turn_on
                - delay: "00:00:05"
                - action: scene.turn_on
                  data: {}
                  target:
                    entity_id: scene.no_two
                - delay: "00:00:05"
                - action: scene.turn_on
                  data: {}
                  target:
                    entity_id: scene.no_three
                - delay: "00:00:05"
                - action: scene.turn_on
                  data: {}
                  target:
                    entity_id: scene.no_four
                - delay: "00:00:05"
mode: restart

This automation is started when the bool is deactivated:

alias: AnwesenheitssimulationAUS
description: "2024-10-20"
triggers:
  - entity_id:
      - input_boolean.anwesenheitssimulation
    to: "off"
    trigger: state
    from: "on"
conditions: []
actions:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.foo
mode: restart

It felt a bit tricky to build the automation in a way, that it is “intercepted” when the value of the input_boolean changes, so hopefully this doesn’t look too bad.

This is my first Home Assistant YAML, so I would really like to hear your thoughts if this code is idiomatic from your point of view. :slight_smile:

Hi, I think this is a leftover from you wanting to put it in one automation before? because only the first trigger does something, so if you remove the second trigger, you also no longer need the choose.

I have something similar, but with some differences.

I have a vacation setting, which means the simulation runs. I do not use the simulation boolean as the trigger, but sunset/sunrise or time. I put the vacation in the conditions, it will not work if vacation is off.

then, to make it less predictable, I use some random delay:

  - delay:
      hours: 0
      minutes: "{{ range(10, 35)|random|int(0) }}"
      seconds: "{{ range(0, 59)|random|int(0) }}"

Thanks for your answer!

No, it isn’t. It is there intentionally, after running into the following issue. :slight_smile:
Explaination:

  • The automation shall (obviously) start when input_boolean.anwesenheitssimulation is set to true.
  • The automation needs (obviously) to stop when input_boolean.anwesenheitssimulation is set to false
  • Issue I ran into without the statement: When input_boolean.anwesenheitssimulation was set to true and some time later to false, the automation AnwesenheitssimulationAN (AN == ON) ran through until the end of the while loop
  • So, I set mode: restart to restart the whole automation when the bool in question is set to false.
  • Because there is no condition set, nothing than a restart of the automation happens, as intended.

This was one of the questions I asked myself: Does this make sense or is there any other mechanic in HA to stop an automation working in an idiomatic way.

Great code, thanks a lot for sharing!

Ah, now I get it. This is rather convoluted, and in a year you’ll probably don’t understand what you did there. The solutions is rather simple. Put the loop in a separate script. use script.turn_on to start it and script.turn_off to stop it. You can use one automation to turn it on and off:

alias: AnwesenheitssimulationAnAus
description: "2024-10-21"
triggers:
  - entity_id: input_boolean.anwesenheitssimulation
    to:
      - "on"
      - "off" 
    trigger: state
actions:
  - action: "script.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: script.anwesendheidssimulation

I would trigger the automation to restore the lights to normal based on the state of the script turning off. The reason being that the script is aborted during a delay, so you’d want to wait until it has actually stopped.

Last but not least, have you considered using scene.create to capture the state of the lights before absence, and restoring that when you stop? It kind of depends how long you intend the boolean to be on, if you start it dat daytime and stop at night, it might not be what you want.

1 Like

Using a script sounds like a great idea. I had completely overseen this possibility. Thanks a lot for the hint!

1 Like