Conditional actions - simulate that someone is home

Hi

I need some support with an addition to my automation.
I turn off all light at 02:00 on friday and saturday evenings, or when I am home on vacation. According to this code:

  • alias: “Turn off lights at weekends”
    trigger:
    platform: time
    at: ‘02:00:00’
    condition:
    condition: or
    conditions:
    - condition: state
    entity_id: ‘binary_sensor.workday_today’
    state: ‘off’
    - condition: state
    entity_id: input_boolean.vacation_mode
    state: ‘on’
    action:
    • service: homeassistant.turn_off
      entity_id: group.all_lamps

But I also would like to add one more input boolean called “away_mode” in the action part.
If it is true them it simulates that someone is how by randomizing 10-20 more minutes before it turns of the lights. If false it simply turns off the lights at 02:00.

How would the action part look like with the additional conditions?

First, it’s always helpful to properly format any code you post so people can read it correctly. When you don’t do this the code gets so messed up it’s hard to read or spot problems. Just precede the code with a line that contains only three back-ticks (the ` character), and follow the code with another line that also just contains three back-ticks.

The thing about a condition in the action part of an automation (or in a script, which is basically the same thing) is if it’s false the rest of the automation/script is aborted. You can’t just use a condition for one step. So, what you probably need to do is to add a delay whose length depends on input_boolean.away_mode. Something like this:

  action:
    - delay: >
        {% if is_state('input_boolean.away_mode', 'off' %}
          00:00:00
        {% else %}
          {{ range(10*60,20*60+1)|random|timestamp_custom("%H:%M:%S",False) }}
        {% endif %}
    - service: homeassistant.turn_off
      entity_id: group.all_lamps

Thanks for your reply.
I get however an error when I insert the code block.
“Invalid config for [automation]: [delay] is an invalid option for [automation]. Check: automation->action->0->delay. (See /config/configuration.yaml, line 169). Please check the docs at https://home-assistant.io/components/automation/

Line 169 refers to my “group: !include groups.yaml” row…

I copied the code exactly as it is. The code works without the code between “action” and “service”…

Any ideas?

You can also check out occusim (Occupancy Simulator) in Appdaemon. I’ve been using that for a few years and it works very well.

This is probably because of a typo. I missed the closing parenthesis for the is_state function. Try this:

  action:
    - delay: >
        {% if is_state('input_boolean.away_mode', 'off') %}
          00:00:00
        {% else %}
          {{ range(10*60,20*60+1)|random|timestamp_custom("%H:%M:%S",False) }}
        {% endif %}
    - service: homeassistant.turn_off
      entity_id: group.all_lamps