My automation only runs when switched on - not if already on over the change in time period

Hi all. I’m very new to the HA environment and am trying to learn how automations work in this environment.

What I’d like to happen is that when my kid goes to bed the living room lights dim, and go back to full brightness when its day time.

The automations and scenes work fine, but only if the lights are being switched on from off. What I’d like is them to change to the appropriate brightness setting even if they’re already on before 7pm bed time. Is this possible? I’ve put my code below.

alias: Living room lights daytime
description: ""
trigger:
  - platform: state
    entity_id:
      - light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light_2
      - light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light
    to: "on"
condition: []
action:
  - if:
      - condition: time
        after: "19:00:00"
        before: "06:00:00"
        weekday:
          - sun
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
    then:
      - type: turn_on
        device_id: 12dee9c57d3d83601db5cc322e4e2909
        entity_id: switch.ikea_of_sweden_tradfri_control_outlet_switch
        domain: switch
      - type: turn_on
        device_id: 622c63408e2d7b8349f0ef0c9a73f900
        entity_id: light.lounge_lamp
        domain: light
        brightness_pct: 50
      - type: turn_on
        device_id: 4f977717c370ccf9846a28d5f3fca633
        entity_id: light.ikea_of_sweden_tradfri_bulb_e27_cws_806lm_light
        domain: light
        brightness_pct: 50
      - type: turn_on
        device_id: e54c2c9da61b0523a0beaf20b0457dd7
        entity_id: light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light_2
        domain: light
        brightness_pct: 50
      - type: turn_on
        device_id: 84ba8a5404ffa5be7125933e064ac199
        entity_id: light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light
        domain: light
        brightness_pct: 50
    else:
      - type: turn_off
        device_id: 622c63408e2d7b8349f0ef0c9a73f900
        entity_id: light.lounge_lamp
        domain: light
      - type: turn_off
        device_id: 12dee9c57d3d83601db5cc322e4e2909
        entity_id: switch.ikea_of_sweden_tradfri_control_outlet_switch
        domain: switch
      - type: turn_off
        device_id: 4f977717c370ccf9846a28d5f3fca633
        entity_id: light.ikea_of_sweden_tradfri_bulb_e27_cws_806lm_light
        domain: light
      - type: turn_on
        device_id: e54c2c9da61b0523a0beaf20b0457dd7
        entity_id: light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light_2
        domain: light
        brightness_pct: 100
      - type: turn_on
        device_id: 84ba8a5404ffa5be7125933e064ac199
        entity_id: light.ikea_of_sweden_tradfribulbe14wscandleopal470lm_light
        domain: light
        brightness_pct: 100
  - if:
      - condition: or
        conditions:
          - condition: sun
            after: sunset
            after_offset: "-00:30:00"
          - condition: sun
            before: sunrise
            before_offset: "00:30:00"
    then:
      - type: turn_on
        device_id: ead662157ac41f93ae4fe4855ec90cac
        entity_id: light.hallway
        domain: light
        brightness_pct: 50
    else:
      - type: turn_off
        device_id: ead662157ac41f93ae4fe4855ec90cac
        entity_id: light.hallway
        domain: light
mode: single

For an automation to run, you need some trigger. This is an event that occurs, either the lignts are turned on (as your automation currently uses) or a certain time is reached (ie 7pm).

As such, for your scenario, what is the event that should tell HA to do the action of dim the lights? If it is that the time is 7pm then use a time trigger.

If you then want this to happen only if the lights are on, add a condition of your ikea lights are set to on.

Your action would then be what you want to happen. Ie dim the lights to 50%.

That is one part of your automation. Then if you want something else to happen at 6am, that would be another trigger to make the automation run. It is simpler to have these as seperate automations while your are learning but you can also add an id to a trigger and use it in an if statement in an action. So you can add and id for the 7pm trigger of night and have your action as if night turn on at 50%. Then your second trigger could be for 6am with an id of day and have another action with if day then set lights tom100%.

Sorry no yaml to copy but i am away from my computer and sometimes guidance can be more helpful when trying to learn.

Hope this helps

Oh that sort of makes sense. In this first instance (i.e. until I get more versed in things) I think I want 3 automations to cover the cases here:

  1. What happens when the lights come on (the automation I have)
  2. What happens at 7pm if the lights are on
  3. What happens at 6am if the lights are on

I reckon I could probably duplicate the code and switch the triggers/conditions.

Thanks, I think thats cleared things up for me (until I end up back here because it didn’t work!)

Had a spare few mins at my computer today, so put together how you could do this in a single automation. There maybe slightly better ways, but wanted it to be fully automation UI compatible for you. As such, you can select view in YAML, paste this in and then go back to view in Visual Editor. You will obviously need to change device_ids/entity ids etc.

EDITED: Sorry slight logic issue and used light.turn_on service as it reads much cleaner in yaml to help understand what is happening.

alias: Lounge Light Brightness By Time
description: |-
  If light turned on between 19:00 and 06:00, only turn on at 30%
  If light turned on at other time turn on at 100%
  If light on at 19:00, dim to 30%
  If light on at 06:00, set to 100%
trigger:
  - platform: device
    type: turned_on
    device_id: 95c4891f8fbafcfb898b84c72125c92d
    entity_id: light.lounge_main_light
    domain: light
    id: LightOn
  - platform: time
    at: "19:00:00"
    id: Night
  - platform: time
    at: "06:00:00"
    id: Morning
condition: []
action:
  - if:
      - condition: trigger
        id: LightOn
    then:
      - if:
          - condition: time
            after: "19:00:00"
            before: "06:00:00"
        then:
          - service: light.turn_on
            data:
              brightness_pct: 30
            target:
              entity_id: light.lounge_main_light
        else:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: light.lounge_main_light
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id: Night
          - condition: state
            entity_id: light.lounge_main_light
            state: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 30
        target:
          entity_id: light.lounge_main_light
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id: Morning
          - condition: state
            entity_id: light.lounge_main_light
            state: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target:
          entity_id: light.lounge_main_light
mode: single

Thanks for this - turns out I’d not been logged in since September!