ScottS
February 8, 2022, 7:47pm
1
I would like to create a script to set light brightness based on sunrise/sunset. Below are the scripts I presently use to change the brightness manually. How can I combine these into 1 script based on sunrise/sunset. I’ll use this as a trigger on my Stream Deck.
office_desk_lamp_on:
alias: Office Desk Lamp On-Day
sequence:
- type: turn_on
device_id: 49dc7efaec0941a1a9a0470e8424c3f7
entity_id: light.office_desk_lamp
domain: light
brightness_pct: 100
mode: single
icon: mdi:lightbulb
office_desk_lamp_on_night:
alias: Office Desk Lamp On-Night
sequence:
- type: turn_on
device_id: 49dc7efaec0941a1a9a0470e8424c3f7
entity_id: light.office_desk_lamp
domain: light
brightness_pct: 80
mode: single
icon: mdi:lightbulb
mk-maddin
(Martin)
February 8, 2022, 8:46pm
2
If you say combine based on sunrise / sunset you mean after sunrise you want to set brightness 100 and after sunset you want to set brightness to 80?
A simple choose with sunstate should do the job then - see: Script Syntax - Home Assistant
ScottS
February 8, 2022, 11:08pm
3
Thank you.
This is what I came up with, but the syntax is wrong. What appropriate changes do I need to make?
# Office Desk Lamp On
office desk lamp on:
- trigger:
- platform: state
entity_id: sun.sun
state: above_horizon
mode: queued
action:
- alias: "Turn on lamp and set brightness"
choose:
# IF (Sun is above horizon)
- alias: "Sun Above Horizon"
conditions: "{{ trigger.to_state.state == 'on' }}"
sequence:
- service: script.turn_on
target:
entity_id:
- script.office_desk_lamp_on_day
# ELSE (Sun is Below Horizon)
default:
- service: script.turn_on
target:
entity_id:
- script.office_desk_lamp_on_night
nickrout
(Nick Rout)
February 8, 2022, 11:21pm
4
what do you mean “the syntax is wrong”? What is telling you so?
ScottS
February 8, 2022, 11:59pm
6
I figured it out…
office_desk_lamp:
alias: Office Desk Lamp
sequence:
- choose:
- conditions:
- condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
sequence:
- type: turn_on
device_id: 49dc7efaec0941a1a9a0470e8424c3f7
entity_id: light.office_desk_lamp
domain: light
brightness_pct: 80
- service: notify.notify
data:
message: Light on, Brightness 80%
default:
- type: turn_on
device_id: 49dc7efaec0941a1a9a0470e8424c3f7
entity_id: light.office_desk_lamp
domain: light
brightness_pct: 100
- service: notify.notify
data:
message: Light on, Brightness 100%
mode: single
icon: mdi:lightbulb
ScottS
February 9, 2022, 2:43am
7
I was premature on success. Although the script executes, it is not conditioning on the sunset. It always executes the Default. HA recognizes the correct time. What am I missing?
nickrout
(Nick Rout)
February 9, 2022, 3:05am
8
You need to go through the automation/script debug.
ScottS
February 9, 2022, 3:15am
9
I’m not understanding the output. Is this saying the condition is not even being first ran?
ScottS
February 9, 2022, 3:25am
10
I believe I found the issue. I needed to have it structured as such…
- conditions:
- condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
Rather than…
- conditions:
- condition: sun
after: sunset
- condition: or
conditions:
- condition: sun
before: sunrise