Having some trouble getting my living room lamp to turn on 30 minutes from sunset when my wife and I are home. I am using trigger to test from the front end. Every time the light turns on but the system sees that one of us is away (not_home).
Automation.yaml:
- alias: Living Room Lamp - Sunset
trigger:
platform: sun
event: sunset
offset: "-00:30:00"
condition:
- condition: state
entity_id: 'device_tracker.XXXXXX'
state: 'home'
- condition: state
entity_id: 'device_tracker.XXXXXX'
state: 'home'
action:
service: homeassistant.turn_on
entity_id: switch.switch
Finally!!
I can try and give something back to the community.
I am definitely no expert by any means but this works for me:
- alias: Dining room light on at dusk if someone is home
trigger:
- platform: template
value_template: '{{states.group.family_presence.state == "home"}}'
- platform: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: 5.0
condition:
condition: and
conditions:
- condition: state
entity_id: group.family_presence
state: "home"
- condition: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: 5.0
- condition: time
before: "23:00:00"
action:
service: homeassistant.turn_on
entity_id: switch.dining_room_light
I use sun elevation rather than sunset as I read that it works better with the varying seasons. I have yet to establish that as I have only had this up and running for a few days.
Iām happy to answer any questions but there are a LOT more knowledgeable people out there (Iām expecting to be told there is a better way to do what I am doing!)
Some pointers in addition to klogg example. Or maybe more explanation how it works
As in klogg post, you could change the condition to work with a group. Make a group with all the devices e.g.
family_devices:
name: Family
entities:
- device_tracker.XXX
- device_tracker.YYY
If one of the entities in the group evaluates āhomeā the group itself evaluates āhomeā.
Or you could use āorā condition instead of āandā which is the default if you dont specify. it would look like below code. With a group you need to change the entity ID to āgroup.family_devicesā. And have only one condition of course.
condition:
- condition: or
conditions:
- condition: state
entity_id: device_tracker.XXXXXX
state: 'home'
- condition: state
entity_id: device_tracker.XXXXXX
state: 'home'
Moving to trigger:
Currently your lamp turns on 30 min before sunset if you are home. But it will not turn on after that point if you were not at home. Like in klogg example, you could add another trigger for this.
Mine is a bit different compared to above post but both work just fine.
- platform: state
entity_id: group.family_devices
to: 'home'
It would look something like below code.
- alias: Living Room Lamp - Sunset
trigger:
- platform: sun
event: sunset
offset: '-00:30:00'
- platform: state
entity_id: group.family_devices
to: 'home'
If you add all persons separately as triggers then I would add one more condition to check if the lamp if already on. The automation would trigger separately when all persons are coming to home and the lamp would go on multiple times. Not necessary any kind of problem but personally I donāt want to do it so
@klogg Canāt think of anything to do better. You have already taken into consideration all my points. Can only think of different ways but not really better ways. But then again, Iām more of hardware guy myself
When you trigger from the front end the condition is ignored and the action is executed.
Remove the quotes from around the entity_ids in your conditions.
And, Iām just checking that you only want this switch on if youāre both home? Thatās what youāve coded. Youāll need either an OR condition or a group if you want it to come on if either of you are home.
Other than that the code is fine. And will trigger at sunset minus 30 minutes every day, and if youāre both home turn the switch on
Thanks all! I did not realize trigger will ignore the condition. Is there a way to test this completely?
For now, I was just testing my first automation so I wanted to see if I was getting the conditions right before setting up as āeither one of us homeā.
Final Code which says āIf either one of us are home (or come home), turn on lamp 30 minutes before sunsetā:
- alias: Living Room Lamp - Sunset
trigger:
- platform: sun
event: sunset
offset: "-00:30:00"
- platform: state
entity_id: group.family_devices
to: 'home'
action:
service: homeassistant.turn_on
entity_id: switch.switch
Ahh, I was missing conditions. Maybe @taikapanu didnāt include conditions in his trigger code.
Basically I only want the light to turn on 30 minutes before sunset when either one of us are home -OR- if we come home after 30 minutes before sunset.
Excellent! Just so I understand correctly, this says turn Triangle lamp on at sunset if someone is home for 1 minute (or comes home) between the hours of 5pm and 11pm?
Not quite but almost. Automation is always triggered if any of the triggers evaluate true. If conditions evaluate true then the action is executed.
So in this case:
IF sunset OR someone has been home for one minute or longer
=> check if conditions evaluate TRUE
Because of the AND all mentioned conditions need to be true
someone needs to be home (family_devices)
time needs to be between 5pm and 11pm
the lamp needs to be off (switch.sonoff1)
=> if all TRUE execute the action
I have then another automation to turn off devices when people leave the house, itās getting bright inside and some other.
#--------------------------------------------------------------
#
# Turn off all devices when everyone has left the house.
# Dog is not counted.
#
#--------------------------------------------------------------
- alias: Leaving home
trigger:
- platform: state
entity_id: group.family_devices
to: not_home
- platform: state
entity_id: group.relative_devices
to: not_home
condition:
- condition: and
conditions:
- condition: state
entity_id: group.family_devices
state: 'not_home'
- condition: state
entity_id: group.relative_devices
state: 'not_home'
action:
- service: script.turn_on
entity_id: script.turn_off_all_devices
And below the script that is calling. I have it like this because I reuse that in few other places.
turn_off_all_devices:
alias: Turn off all devices
sequence:
- alias: turn off (kitchen)
service: homeassistant.turn_off
entity_id: group.kitchen_lights
- alias: turn off (living room)
service: homeassistant.turn_off
entity_id: group.living_room_lights
- alias: turn off (decorative lights)
service: homeassistant.turn_off
entity_id: group.common_decorative_light
- alias: turn off (all bedroom lights)
service: homeassistant.turn_off
entity_id: group.all_bedroom_lights
- alias: pause (spotify)
service: media_player.media_pause
entity_id: media_player.spotify
- alias: turn off (hallway)
service: homeassistant.turn_off
entity_id: switch.hallway
- alias: turn off (bedroom)
service: homeassistant.turn_off
entity_id: switch.bedroom
I want to turn the light on 30 minutes before sunset if one/both of us is home. If one/both of us comes home (both being away) after sunset, turn on the light.
Current code:
- alias: Living Room Lamp - Sunset
trigger:
- platform: sun
event: sunset
offset: "-00:30:00"
- platform: state
entity_id: group.family_devices
to: 'home'
condition:
condition: and
conditions:
- condition: state
entity_id: group.family_devices
state: 'home'
- condition: sun
after: sunset
action:
service: homeassistant.turn_on
entity_id: switch.switch
Presuming it is past half-an-hour to sunset where you are, set your device trackers to away using the device tracker āseeā service. Then so far as your system is concerned, youāre out.
Then push an update from your device trackers, your system thinks youāve come home and runs the automation.