I’m somewhat new to Home Assistant, been reading and working with Jinja2 to try and understand the syntax for templating and have what I believe is what I want, but I don’t know how to execute it.
To summarize, I would like for a trigger of someone at my front door (nest detects person) during the hours of sunset and sunset + 2h 30m to perform an action, turn on a night light. Basically, when someone walks up to the front door between these times it enables a light, essentially an overly complicated motion sensor. lol
What I’ve got is some code I’ve tested in the template builder, and it works, but I’m not sure how to connect this up to the automation, or if there is a better way to do this within the automation that doesn’t require this much code?
{% set time_json = {
"LocalizedTimePlusTwoHours": now() | as_timestamp() + 2.5 * 60 * 60 ,
"LocalizedTime": now() | as_timestamp() | timestamp_local(),
"SunsetTime": state_attr("sun.sun","next_setting") | as_timestamp() | timestamp_local()
}
%}
{% if time_json.LocalizedTime > time_json.SunsetTime and time_json_LocalizedTime < time_json.LocalizedTimePlusTwoHours | timestamp_local() %}
Inside Time window.. do whatever
{%- else -%}
Outside time window do something else.
{%- endif %}
alias: front_door_night_light
trigger:
platform: state
entity_id: sensor.your_nest_motion_sensor
to: 'on'
condition:
- condition: sun
after: sunset
- condition: sun
before: sunset
before_offset: '02:30:00'
Note that the second condition can be a bit confusing at first. It means you want it to be true when the time is before a given time, and the given time is sunset plus a positive offset of 2.5 hours. So both conditions taken together mean the time must be after sunset andbefore 2.5 hours past sunset.
EDIT: Although the docs don’t seem to imply this, looking at the code, I believe this will work as well:
alias: front_door_night_light
trigger:
platform: state
entity_id: sensor.your_nest_motion_sensor
to: 'on'
condition:
condition: sun
after: sunset
before: sunset
before_offset: '02:30:00'
So, again, after sunset andbefore sunset + 2.5 hours.
Thank you so much for the response. I suppose this is not able to added through the GUI and would require adding it to the automations.yaml file directly. Also is it bad to use the wait service for say 60 seconds and then afterwards call the turn_off service?
This looks like an interesting solution, nearly no code needed. Now if I wanted to inverse the logic so only after 2.5hrs after Sunset until sunrise could this be flipped around, it changes the day at 24:00 so I’m wondering if it still works.
It’s not bad, but using a separate automation prevents problems if the automation re-triggers while waiting in the delay (if that happens it just skips the delay and moves to turning the light off).
Thank you for the comments and assistance, super appreciate it. I love HA and am trying to master all the features so I can really leverage all it’s abilities
condition:
condition: or
conditions:
- condition: sun
after: sunset
after_offset: '02:30:00'
- condition: sun
before: sunrise
Not sure why the sun condition can’t do after sunset and before sunrise (meaning the period that spans midnight) like the time condition can. Maybe I’ll try to fix that, er, improve the sun condition.
I’ve implemented both of the sun condition solutions. I’ll check it soon to confirm it’s working and let you know should you be interested. It’s a little busy, but it’s my night light, an announcement of someone at the front door and it works a little differently (stays blue) during my kind of twilight time up to 2 1/2 hours after sunset.
Here is my config…
For daytime only.
alias: ANNOUNCE - DAYTIME - Someone at Front Door
trigger:
- entity_id: binary_sensor.front_door_camera_person_detected
from: 'off'
platform: state
to: 'on'
condition:
- after: sunrise
condition: sun
- before: sunset
condition: sun
action:
- data:
message: Nest saw someone at the front door.
title: Person Detected
service: persistent_notification.create
- data:
entity_id: media_player.great_room_speaker
message: Nest saw someone at the front door.
service: tts.google_say
- timeout: 00:00:10
wait_template: ''
- data:
entity_id: media_player.great_room_speaker
volume_level: '.7'
service: media_player.volume_set
initial_state: true
From sunset to 2 1/2 hours afterward… basically the time when my exterior lights are on.
alias: ANNOUNCE - NIGHTTIME BEFORE LIGHTS OUT - Someone at Front Door
trigger:
- entity_id: binary_sensor.front_door_camera_person_detected
from: 'off'
platform: state
to: 'on'
condition:
- after: sunset
condition: sun
- before: sunset
before_offset: +02:30:00
condition: sun
action:
- data:
entity_id: media_player.great_room_speaker
message: Nest saw someone at the front door.
service: tts.google_say
- data:
message: Nest saw someone at the front door.
title: Person Detected
service: persistent_notification.create
- data:
entity_id: media_player.great_room_speaker
volume_level: '.7'
service: media_player.volume_set
- data:
brightness: '255'
color_name: WHITE
entity_id: light.front_porch
power: 'true'
service: light.lifx_set_state
- timeout: 00:00:30
wait_template: ''
- data:
brightness: '30'
color_name: BLUE
entity_id: light.front_porch
power: 'true'
service: light.lifx_set_state
For nighttime after all exterior lights are off.
alias: ANNOUNCE - NIGHTTIME AFTER LIGHTS OUT - Someone at Front Door
trigger:
- entity_id: binary_sensor.front_door_camera_person_detected
from: 'off'
platform: state
to: 'on'
condition:
- after: sunset
after_offset: +02:30:00
condition: sun
- before: sunrise
condition: sun
action:
- data:
entity_id: media_player.great_room_speaker
message: Nest saw someone at the front door.
service: tts.google_say
- data:
entity_id: media_player.great_room_speaker
volume_level: '.7'
service: media_player.volume_set
- data:
message: Nest saw someone at the front door.
title: Person Detected
service: persistent_notification.create
- data:
brightness: '255'
color_name: WHITE
entity_id: light.front_porch
power: 'true'
service: light.lifx_set_state
- timeout: 00:00:30
wait_template: ''
- data:
entity_id: light.front_porch
service: homeassistant.turn_off
So the suggestions I was given for the timespan 2.5 hours after sunset and before sunrise don’t seem to be tiggering.
alias: ANNOUNCE - NIGHTTIME AFTER LIGHTS OUT - Someone at Front Door
trigger:
- entity_id: binary_sensor.front_door_camera_person_detected
from: 'off'
platform: state
to: 'on'
condition:
- after: sunset
after_offset: +02:30:00
condition: sun
- before: sunrise
condition: sun
action:
- data:
entity_id: media_player.great_room_speaker
message: Nest saw someone at the front door.
service: tts.google_say
I also tried another condition but no luck, it’s like it’s not detecting the Sunrise tomorrow or the sun below horizon tomorrow, and I tied putting before 06:00 and that doesn’t work either.
alias: ANNOUNCE - NIGHTTIME AFTER LIGHTS OUT - Someone at Front Door
trigger:
- entity_id: binary_sensor.front_door_camera_person_detected
from: 'off'
platform: state
to: 'on'
condition:
- after: sunset
after_offset: +02:30:00
condition: sun
- condition: state
entity_id: sun.sun
state: below_horizon
action:
- data:
entity_id: media_player.great_room_speaker
message: Nest saw someone at the front door.
service: tts.google_say
I would greatly appreciate any help understanding why this doesn’t work.
The other problem you likely ran into (or would have run into if the logic was right) is not quoting the offset times. This:
after_offset: +02:30:00
needs to be:
after_offset: '+02:30:00'
or:
after_offset: '02:30:00'
When you entered it in the more JSON-like format you quoted the times, so that’s another thing that was different.
EDIT: … but I’m not 100% sure about this. I know there are cases where not quoting a time can lead to errors, but in some cases you can get by without quotes. I tend to play it safe and always quote times.
In this and most cases yes. One exception is numbers, they should not be quoted, e.g. below: 30 If you quote a number it becomes a string.
The UI is an introduction to automation. It can do a lot but not everything. For advanced automation you will need to learn YAML and a bit of Jinja. Like most things it will become second nature if you practice at it. There are plenty of examples in the docs and people here are always willing to help.
Or perhaps you might like NodeRed. I’m not a fan but plenty of people prefer it.