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.
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.
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…
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…
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