I already have a script (created with the help on this forum) that when activated will run 1 of 2 different scripts depending on the position of the sun
Yes, either by tapping a button on my phone, or via Google Home, Alexa or Siri.
But Iâm all for an easier way than tweaking my existing script if there is one
Basically the script run before sunset turns on the Xbox and TV, setâs the correct HDMI channel, whereas the script for after sunset does the same but also turns on specific lights.
I have a switch setup to turn it all on and off, yes. I use this for Google Home, Alexa and Siri so I can say âturn on the Xboxâ or âturn off the Xboxâ.
Ok, so iâm guessing that you just want to turn on and off a light or series of lights if itâs nighttime when you press the xbox button?
get rid of the 2 scripts, only run one. Remove the lighting/uniqueness from the night time script. add it to the automation with a condition:
automation:
- alias: Turn on light after dark with xbox
trigger:
- platform: state
entity_id: switch.xbox
to: 'on'
condition:
condition: or # 'when dark' condition: either after sunset or before sunrise
conditions:
- condition: sun
after: sunset
after_offset: "-00:30:00"
- condition: sun
before: sunrise
action:
// turn your lights on and unique to nighttime stuff here.
EDIT: You are welcome to try and accomplish your task with a value_template, but adjusting time is a pain in the ass when you can just build your automation off an event.
EDIT: Just say the word if you want to try to do the value_template route, I can help with that as well.
I agree with @petro, from how you describe it, it sounds like your code could use some reorganization. Also, trying to use sun.sunâs sunrise & sunset times is problematic because as soon as time crosses one, it jumps to the next day, so basically now() is always before both of them.
But, in the meantime, based on this:
Iâd say, just have one script instead of two. The script does everything the âbeforeâ script did, then adds a condition that itâs after sunset, followed by the steps that turn on the specific lights. Something like:
script:
xbox:
sequence:
# Turn on Xbox & TV, set correct HDMI channel...
...
# Only do remaining steps if it's after 30 min before sunset
- condition: sun
after: sunset
after_offset: '-00:30:00'
# Turn on specific lights...
...