Help With Conditional Script

I’d like to be able to tell Alexa to turn the lights on, but depending on the time of day, a different action is taken.

E.g. in the morning when it’s still dark, the lights should come on slowly. If it’s during the day, the lights should come on straight away. And if it’s the evening, the lights should do something different again.

I can acomplish this by defining a “morning lights” script and an “evening lights” script etc and expose that to Alexa over an emulated hue bridge, then ask alexa to turn on the “morning lights”, but I’d rather not have to have different scripts and just ask Alexa to turn on the lights and HA does the rest.

Expose an input_boolean.

Tell Alexa to switch the boolean on.

Have an Automation that fires when the boolean is switched on that triggers the appropriate scene depending on the time of day and then switches the boolean back off.

1 Like

Thanks, i’m very much a beginner with this so it took a while to get something working.

What I did was expose the following script to Alexa:

lights_on:
  alias: Alexa Lights On Script
  sequence:
  - service: input_boolean.toggle
    data:
      entity_id: input_boolean.alexa_lights_on

I then created the following automation that runs when the alexa script changes the input_boolean from off to on. It checks if it’s breakfast time, it it is it runs the lights on breakfast script else it runs the general lights on script.

- alias: 'Alexa Lights Automation On'
  trigger:
    platform: state
    entity_id: input_boolean.alexa_lights_on
    from: 'off'
    to: 'on'
  action:
    - service: script.turn_on
      data_template:
        entity_id: >
          {% if (states.sensor.time.state < "06:50") and (states.sensor.time.state > "06:00") %}
            script.breakfast_lights
          {% else %}
            script.teatime_lights
          {% endif %}

I then created the following automation which runs when the input_boolean changes from on to off:

- alias: 'Alexa Lights Automation Off'
  trigger:
    platform: state
    entity_id: input_boolean.alexa_lights_on
    from: 'on'
    to: 'off'
  action:
    - service: script.turn_on
      data:
        entity_id: script.lights_off

It seems to work so far…