I have an automation which is designed to run between certain times of the day (when the sun comes through a window in my flat) which shuts my automated curtains to block glare on my TV. I have use the sun condition else where when closing the curtains in the evening as this is an easier case. However when I want to shut them in a time range using the sun the logic is a bit more difficult. I also have additional conditions to only shut when sunny or partly cloudy.
I have tweaked this endlessly and I think I have the logic right not but still unsure. In this example I want to shut the curtain in a window of 90 mins prior to 80 mins before sunset. My code exert is below:
alias: Stop TV Glare
description: ''
trigger:
- platform: state
entity_id: weather.met_office_xxx
to: sunny
- platform: sun
event: sunset
offset: '-02:50:00'
- platform: state
entity_id: weather.met_office_xxx
to: partlycloudy
- platform: device
device_id: xxx
domain: media_player
entity_id: media_player.sony_bravia_tv
type: turned_on
condition:
- condition: and
conditions:
- condition: and
conditions:
- condition: sun
after: sunset
after_offset: '-02:50:00'
- condition: sun
before: sunset
before_offset: '-01:20:00'
- condition: or
conditions:
- condition: state
entity_id: weather.met_office_xxx
state: sunny
- condition: state
entity_id: weather.met_office_xxx
state: partlycloudy
- condition: device
device_id: xxx
domain: media_player
entity_id: media_player.sony_bravia_tv
type: is_on
action:
- device_id: xxx
domain: cover
entity_id: cover.lounge_curtain_right
type: set_position
position: 0
mode: single
I am unsure on the correct syntax for the sun condition, I think AND is correct as I want it to only close in a specific window prior to sunset.
If anyone has something similar or is able to confirm my assumption as I have read the guidance here Conditions - Home Assistant and I think its correct.