How do I create an automation that checks sun state and presence state and activates specific scenes accordingly throughout the day

Hi there!

I’m super new to home assistant and am really starting to get comfortable with YAML.

I’m trying to build an automation – somewhat similar to the (Routine Director app in smart things) that checks what if the sun is up or down, checks presence and then activates specific scenes accordingly. Additionally it would be great if it could also have a condition for the time of night after the sun is set to activate a sleep scene.

I’ve managed to get booleans setup for presence detection using NodeRed (and they seem to functioning correctly) but I’m a bit stuck after that.

Additionally I’m unsure of how to trigger an automation that I essentially want to run all the time.

I tried to build this out using automations UI (I know it’s a n00b way to do it) but it appears that my logic isn’t correct because this automation isn’t doing anything.

Any help or advice would be great.

Here’s the code:

alias: Routine Director
description: ''
trigger:
  - platform: time_pattern
    seconds: '5'
condition: []
action:
  - choose:
      - conditions:
          - condition: sun
            before: sunset
            after: sunrise
          - condition: state
            entity_id: input_boolean.anyonehome
            state: 'on'
            for: '3'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_at_home
            data:
              transition: 5
      - conditions:
          - condition: sun
            before: sunset
            after: sunrise
          - condition: state
            entity_id: input_boolean.anyonehome
            state: 'off'
            for: '3'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_away
            data:
              transition: 5
      - conditions:
          - condition: sun
            before: sunrise
            after: sunset
          - condition: state
            entity_id: input_boolean.anyonehome
            state: 'on'
            for: '3'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.candle_light
            data:
              transition: 0
      - conditions:
          - condition: sun
            before: sunrise
            after: sunset
          - condition: state
            entity_id: input_boolean.anyonehome
            state: 'off'
            for: '3'
        sequence:
          - service: scene.turn_on
            data:
              transition: 5
            target:
              entity_id: scene.night_away
    default:
      - service: scene.turn_on
        target:
          entity_id: scene.daytime_away
        data:
          transition: 5
mode: single
max: 10

Please try something like this.

alias: New Automation
description: ''
mode: single
trigger:
  - platform: state
    entity_id: input_boolean.anyonehome
    for: '00:03:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: input_boolean.anyonehome
                state: 'on'
              - condition: sun
                before: sunrise
                after: sunset
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_at_home
            data:
              transition: 5
      - conditions:
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
                after: sunset
              - condition: state
                entity_id: input_boolean.anyonehome
                state: 'off'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.daytime_away
            data:
              transition: 5
    default: []

Its not complete but I think you will get the idea. The idea is we start the automation with the trigger any state change in the input_boolean for 3 minutes contionusly. That means when the state changes of input boolean happens and this state continues to be there for 3 minutes, the automation will be triggered. this is better than checking every 5 minutes.
Also please see the format in which for time template is made.

Now with actions, the choose type of action is the right one but you need to use and condition so that both sun and state of input boolean is considered to take the decision.

Also understand that there are many methods to achieve this. The above is one of the simple ones, You can also cut short the entire thing using templates but it would be need more expertise.

1 Like

this way is better,no coding requirment check the video

1 Like

Thank you this is super helpful!

1 Like

Appreciate your help. I can’t get the automation to enable. is it possible for you to check my Syntax. Very much appreciate it!

- alias: Routine Director
  description: ''
  mode: single
  trigger:
    - platform: state
      entity_id: input_boolean.anyonehome
      for: '00:03:00'
  condition:
    action:
    - choose:
      - conditions:
        - condition: sun
          before: sunset
          after: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
          for: '3'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.daytime_at_home
          data:
            transition: 5
      - conditions:
        - condition: sun
          before: sunset
          after: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'off'
          for: '3'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.daytime_away
          data:
            transition: 5
      - conditions:
        - condition: sun
          before: sunrise
          after: sunset
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
          for: '3'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.candle_light
          data:
            transition: 5
      - conditions:
        - condition: time
          after: 00:05
        - condition: sun
          before: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
          for: '3'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.good_night
          data:
            transition: 5
      - conditions:
        - condition: sun
          before: sunrise
          after: sunset
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'off'
          for: '3'
        sequence:
        - service: scene.turn_on
          data:
            transition: 5
          target:
            entity_id: scene.night_away
      default: []

I have seen that you have corrected the trigger part but there are syntax issues in rest of the automation. Please see the example I have given. Especially the action part. We have to use and condition there and also for condition is not needed. Please see the example…

1 Like

The for option can be defined like this:

for:
  minutes: 3

or like this:

for: '00:03:00'
1 Like

Thanks you!

Okay I tried to fix it but it still won’t work:

- alias: Routine Director
  trigger:
  - platform: state
    entity_id: input_boolean.anyonehome
    for: '00:03:00'
  condition:
    action:
    - choose:
      - condition: and
        conditions:
        - condition: sun
          before: sunset
          after: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.daytime_at_home
          data:
            transition: 5
      - condition: and
        conditions:
        - condition: sun
          before: sunset
          after: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'off'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.daytime_away
          data:
            transition: 5
      - condition: and
        conditions:
        - condition: sun
          before: sunrise
          after: sunset
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.candle_light
          data:
            transition: 5
      - condition: and
        conditions:
        - condition: time
          after: '00:05'
        - condition: sun
          before: sunrise
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'on'
        sequence:
        - service: scene.turn_on
          target:
            entity_id: scene.good_night
          data:
            transition: 5
      - condition: and
        conditions:
        - condition: sun
          before: sunrise
          after: sunset
        - condition: state
          entity_id: input_boolean.anyonehome
          state: 'off'
        sequence:
        - service: scene.turn_on
          data:
            transition: 5
          target:
            entity_id: scene.night_away
      default: []

How are you testing it and what exactly fails to work?

I believe the syntax is wrong. Does it pass Check Configuration? (Configuration > Server Controls > Check Configuration).


EDIT

Confirmed; the syntax is invalid. I just checked it on my system and all the conditions in choose are incorrect.

Change every instance of this:

      - condition: and
        conditions:

to this:

      - conditions:
1 Like

Made that change. Not sure what I’m doing wrong but I keep getting this and I cant turn it on and it doesn’t seem to work.

Me neither. I don’t know what you did to produce that result.

So I managed to get it to be accepted in home assistant. But the automation doesn’t do anything ha. What a fail.

I’ve tried to trigger it by “leaving the house” (my presence sensors use Wifi). The are triggering correctly and so is the boolean to check if people are home or not.

Appreciate the help but I’m not sure what I’m doing wrong still…

Here’s my final code:

alias: Routines Manager
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /3
  condition: []
  action:
  - choose:
    - conditions:
      - condition: sun
        before: sunset
        after: sunrise
      - condition: state
        entity_id: input_boolean.anyonehome
        state: 'on'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.daytime_at_home
        data:
          transition: 5
    - conditions:
      - condition: sun
        before: sunset
        after: sunrise
      - condition: state
        entity_id: input_boolean.anyonehome
        state: 'off'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.daytime_away
        data:
          transition: 5
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: 00:05
      - condition: state
        entity_id: input_boolean.anyonehome
        state: 'on'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.candle_light
        data:
          transition: 5
    - conditions:
      - condition: time
        after: 00:06
      - condition: sun
        before: sunrise
      - condition: state
        entity_id: input_boolean.anyonehome
        state: 'on'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.good_night
        data:
          transition: 5
    - conditions:
      - condition: time
        after: 00:06
      - condition: sun
        before: sunrise
      - condition: state
        entity_id: input_boolean.anyonehome
        state: 'off'
      sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.night_away
        data:
          transition: 5
    default: []
  mode: single

If your system restarts after the trigger, it can cause issues with this check, at least that was my experience some time ago and the reason I changed the condition check. I find this way of checking if the Sun is up/down works more reliably as the Sun is always one of these.

- condition: state
  entity_id: sun.sun
  state: "below_horizon"

- condition: state
  entity_id: sun.sun
  state: "above_horizon"

Have you tried splitting the home and away automations into separate automations? I don’t use the auto generated HA editor because it jumbles the code, so I could be wrong looking at your automation, but it seems as though you have 2 different automations going on under one trigger.

I can see you are using the choose option, but it may be easier to split this into 2 different automations just to get things working first, then work on combining them.

I’m not great with scripts and so on, so my automations are little long, but this might help point you in the right directions. Here are 2 of mine for leaving/arriving home with a Sun condition.

# ARRIVING HOME - TURN ON ITEMS IF SUNSET #
- alias: Arriving Home - Turn Items On If Sunset
  initial_state: true
  trigger:
    platform: state
    entity_id: group.family
    from: "not_home"
    to: "home"
    for:
      seconds: 5
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ state_attr("sun.sun", "elevation") < 8 }}'
      - condition: time
        before: "23:59:59"
  action:
    - service: homeassistant.turn_on
      entity_id:
        - script.kitchen_normal
        - script.blue_hall
    - service: script.turn_on
      data_template:
        entity_id: >
          {% if is_state('person.jason', 'home') %}
            script.blue_lights
          {% elif is_state('person.kym', 'home') %}
            script.pink_lights
          {% elif is_state('device_tracker.carolyn', 'home') %}
            script.white_lights
          {% elif is_state('device_tracker.chuckie', 'home') %}
            script.blue_lights
          {% endif %}
    - delay: "00:01:30"
    - service: notify.alexa_media
      data_template:
        target: media_player.lounge
        data:
          type: tts
        message: "Hello"
    - delay:
        seconds: 1
    - service: notify.alexa_media
      data_template:
        target: media_player.lounge
        data:
          type: tts
        message: '{{ ["Welcome home", "it is good to see you again", "I hope you have had a good day so far", "Welcome back home", "It is nice you are back, I was missing you"] | random }}'

and

# LEAVING HOME - CHANGE THE STEPS & TABLE #
- alias: Leaving Home - Change the Steps & Table
  initial_state: true
  trigger:
    - platform: state
      entity_id: group.family
      from: "home"
      to: "not_home"
      for:
        seconds: 65
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.family
        state: "not_home"
      - condition: template
        value_template: '{{ state_attr("sun.sun", "elevation") < 10 }}'
      - condition: time
        before: "23:59:59"
  action:
    - service: script.turn_on
      entity_id: script.white_lights