Automation problem!

Hi

I have many automations running fine and I have just set up an automation to turn on my blue bedroom lights on and after a few seconds to turn off the bedroom lights all together if the state of the bedroom lights is “on”.

I have a script that turns on the bedroom lights initially and then the script stops running leaving the bedroom lights turned on with state ‘on’.

Here is my automation:

- id: bedroom_lights_train_off
  alias: Bedroom Lights Train Off
  trigger:
    - platform: template
      value_template: "{% if is_state('switch.bedroom_lights', 'on') %}true{%
endif %}"
  condition:
    - condition: time
      after: '07:51:00'
      before: '07:53:00'
  action:
    - delay: '00:00:02'
    - service: switch.turn_on
      entity_id: switch.blue_bedroom_lights
    - delay: '00:00:03'
    - service: switch.turn_off
      entity_id: switch.bedroom_lights

So in the morning between 7:51:00 and 7:53:00 I expect the above automation to turn the bedroom lights to blue i.e. turn on the blue bedroom lights before switching off the bedroom lights all together. However, nothing happens and my bedroom lights continue to remain turned on with none of the action(s) taking place.

When I trigger the above automation for testing purposes without turning the bedroom lights on i.e. bedroom lights have a state of “off” in this case, the action(s) takes place i.e. the blue bedroom lights are triggered on and I assume the next action which is to turn off the bedroom lights all together also takes place.

Can anyone help me with this? All I am trying to do is when the bedroom lights are on between 7:15:00 and 7:50:00, the blue bedroom lights should turn on and then the bedroom lights should turn off all together.

Thanks.

Why can’t you use a standard state is on trigger? rather than a template? If you trigger manually the state of the switches doesn’t matter as only the action takes place. Why do you assume the second action takes place? it’s only 3 seconds later!

OK I see why you are doing the template, however I think it will only be evaluated when it changes so if the light is on before the conditions time I expect it will never trigger. So I expect you will need to run an automation trigger on time per min and then the condition of time and state of lights being on.

For instance…

 - alias: 'Wake Me Up'
   trigger:
     - platform: time
       minutes: '/1'
       seconds: '0'
#        at: states.sensor.alarm_time.state
   condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'on'
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
                - sat
            - condition: template
              value_template: '{{ now().time().strftime("%R") == states.sensor.alarm_time.state }}'
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
            - condition: template
              value_template: '{{ now().time().strftime("%R") == states.sensor.alarm_time.state }}'
   action:
#      - service: notify.mypushbullet
#        data:
#          title: "Alarm"
#          message: "Good morning Sir. Time to Wake Up!"
      - service: switch.turn_on
        data:
          entity_id:
            - switch.pibotlamps
            - switch.pibedroomceilingdimmer

as an example.

That automation will only run if switch.bedroom_lights turns on, between 07:51:00 and 07:53:00. If the lights are already on, it won’t trigger.

If you want it to trigger if the lights are already on, you’ll want to add a second trigger, and conditions. Something like:

- id: bedroom_lights_train_off
  alias: Bedroom Lights Train Off
  trigger:
    - platform: state
      entity_id: switch.bedroom_lights
      to: 'on'
    - platform: time
      at: '07:51:00'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '07:51:00'
        before: '07:53:00'
      - condition: state
        entity_id: switch.bedroom_lights
        state: 'on'
  action:
    - delay: '00:00:02'
    - service: switch.turn_on
      entity_id: switch.blue_bedroom_lights
    - delay: '00:00:03'
    - service: switch.turn_off
      entity_id: switch.bedroom_lights
3 Likes

Thanks @Tinkerer works flawlessly now.

Hi @Tinkerer

I am now trying to turn on my kitchen lights between 09:00:00 and 09:05:00. The kitchen lights are in the off state prior 09:00:00. I have used the code you provided and changed the state to off but unfortunately the lights do not turn on at all.

Here is my code:

- id: kitchen_lights_on
  alias: Kitchen Lights On
  trigger:
    - platform: state
      entity_id: switch.kitchen_lights
      to: 'off'
    - platform: time
      at: '09:00:00'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '09:00:00'
        before: '09:05:00'
      - condition: state
        entity_id: switch.kitchen_lights
        state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.kitchen_lights

Can you please help? Thanks.

Hey, so to check, you want to run that automation:

  • Any time the lights are turned off - and then check that the time is after ‘09:00:00’ and before ‘09:05:00’
  • At ‘09:00:00’, when the lights are off

I wonder if the problem is that ‘09:00:00’ when the event triggers, isn’t after ‘09:00:00’. What if you change that to ‘08:59:00’?

The same sanity check though, if you manually trigger the automation in the time window, does it work? If the lights are on, and you turn them off during that time, do they turn back on again?

Sorry I meant I am trying to turn on my kitchen lights between 09:00:00 and 09:05:00

What, you just want it to turn on at 09:00?

- id: kitchen_lights_on
  alias: Kitchen Lights On
  trigger:
    platform: time
    at: '09:00:00'
  condition:
    condition: state
    entity_id: switch.kitchen_lights
    state: 'off'
  action:
    service: switch.turn_on
    entity_id: switch.kitchen_lights
1 Like

For my information what’s the point of the condition?

Purely so that if the lights are already on, the automation doesn’t try to turn them on again.

It’s not strictly required, but you’re reducing the overheads.

yes thats correct ! Thanks once again for your help buddy!