Run different scripts before and after sunset?

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

script:
  xbox:
    sequence:
      - service: script.turn_on
        data_template:
          entity_id: >
            {% if states.sun.sun.attributes.elevation > 10 %}
              script.xbox_before_sunset
            {% else %}
              script.xbox_after_sunset
            {% endif %}

I want to change it so that instead of using the sun elevation it uses the sunset time.

So if it’s before sunset -00:30:00 it runs ‘script.xbox_before_sunset’, and after that sunset time it runs ‘script.xbox_after_sunset’


Anyone know how to do this? :slightly_smiling_face:

I think you may be going about this wrong. How are you firing this script? Is this a button you press?

EDIT: let me rephrase, you arn’t going about this wrong, but there may be a much easier solution.

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 :slight_smile:

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.

do you have an ‘off’ sequence for your xbox by chance?

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’.

switch:
  xbox:
    value_template: "{{ is_state('xbox', 'on') }}"
    turn_on:
      - service: script.turn_on
        entity_id: script.xbox
    turn_off:
      - service: script.turn_on
        entity_id: script.xbox_and_tv_off

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.

1 Like

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...
      ...

yah, that would work too.

EDIT: The timing of lights turning on and xbox turning on would be sequential, but you already have that, so it wouldn’t be much of a change.

That’s great, thanks! I never thought to add an automation to an existing event (in this case a switch) :grinning: