Help - LIFX script and automation stopped working?

Hello,

So several years ago, you guys help me write the script for LIFX automation. It was basically an automation for the LIFX to run a light script on a specific day and time.

I was happy with it and it’s been working for years, that I just left it as it was and never touched it, until today I noticed it wasn’t working anymore.

Do you think the recent updates of the the core or the entire Hassio broke the script and automation? Because this is the only thing I only thing I do, keeping the Hassio up to date.

So to help you understand what the automation do. The LIFX bulbs are triggered to run specific color patterns depending on the date. Let’s say for Halloween, it will trigger specific color patterns on repeat, from 17:30 till the next day at 4:00.

Here’s my code:
Automation:

#Halloween Lights Theme   
- id: halloweenlights_theme
  alias: Halloween Lights Theme
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-20'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-21'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-22'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-23'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-24'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-25'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-26'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-27'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-28'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-29'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-30'' and is_state(''sensor.time'', ''17:30'')}}'
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''10-31'' and is_state(''sensor.time'', ''17:30'')}}'
  action:
  - service: script.turn_on
    data:
      entity_id: script.halloweenlights_s
  - service: notify.ios_phones
    data:
      title: Home Automation Message
      message: Halloween Lights Theme has started.

Script:

#Halloween Theme
halloweenlights_s:
  alias: Halloween Lights
  sequence:
    #sequence 1
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 2
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 3
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - delay: '00:00:05'
    #sequence 4
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - delay: '00:00:05'
  - service: script.themesloop_s
    data:
      entity_id: script.halloweenlights_s

#Themes Loop Code
themesloop_s:
  alias: Themes Loop
  sequence:
  - delay: '00:00:00'
  - service_template: >
      {% if states('sensor.time') > '16:00' or states('sensor.time') < '04:00' %}
        {{ entity_id }}
      {% elif is_state('script.light_themes_off_s', 'off') %}
        script.light_themes_off_s
      {% endif %}

#Themes Auto Off
light_themes_off_s:
  alias: Light Themes Off
  sequence:
    - service: light.turn_off
      entity_id:
        - light.balcony_left
        - light.balcony_right
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Holiday Themes Lights has ended.

It was supposed to run starting yesterday, but I just realized, it’s not working anymore. I checked the codes, and they all seem to be ok. Also the iOS notification isn’t working at all too.

Do you think it’s because of the recent updates? Should I just roll it back to the previous version?

Also if I execute the halloween lights manually, the lights script won’t loop anymore, it will run only once, and stop. It seemed like the “themes loop” get triggered, but it’s not repeating the lights theme. I don’t know what broke the entire script, because I haven’t edited this until I got it all working as I want them to be.

Thanks in advanced.

You don’t know why it broke because you have not been reading the release notes or breaking changes and have been upgrading blindly.

Automations and scripts changed significantly in v0.113. There are also new and easier ways to loop scripts.

This is untested it may require a few adjustments before it works according to your requirements.

Version 0.113 introduced the ability to perform looping so the technique you used is now outdated. Here’s the new technique employed in an automation along with a more compact means of triggering the automation at the appropriate time.

- alias: Halloween Lighting
  trigger:
    - platform: time
      at: '17:30:00'
  condition: '{{ now().month == 10 and 20 <= now().day <= 31 }}'
  action:
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has started.
    - repeat:
        while: "{{ now().hour > 16 or now().hour < 4 }}"
        sequence:
          - service: script.halloweenlights_s
    - service: light.turn_off
      entity_id:
        - light.balcony_left
        - light.balcony_right
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has ended

Your halloweenlights_s script remains almost the same except you need to remove the last few lines that called the themesloop_s script. The new technique no longer requires the themesloop_s script or the light_themes_off_s script.

#Halloween Theme
halloweenlights_s:
  alias: Halloween Lights
  sequence:
    #sequence 1
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 2
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 3
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - delay: '00:00:05'
    #sequence 4
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - delay: '00:00:05'

To test it, you can either wait until this evening or simply modify the automation in two places to allow it to trigger earlier:

  • Change the trigger time from 17:30:00 to an earlier time.
  • Change the time range within the condition to ensure it includes the earlier time: (now().hour > 16 or now().hour < 4)

EDIT

I overlooked to mention that the automation uses another new feature which is a streamlined way of defining a Template Condition.

Instead of this:

  - condition: template
    value_template: "{{ whatever }}"

it can be written like this:

  - condition: "{{ whatever }}"

EDIT 2

Correction. Template condition was invalid; lacked required brackets and needlessly checked script’s state. Also modified the initial condition so that it is all on the same line.

Yes. That’s what I thought. And it was my bad. I just never expected a big change in recent updates. I will be careful next time.

Thank you for showing me the new code technique. So it looks a lot easier to understand and way shorter to write.

I have a question about the condition. Is how you wrote it really correct?

- '{{ now().month == 10 and 20 <= now().day <= 31 }}'

I just feel like… should it be like…

- '{{ now().month == 10 and 20 >= now().day <= 31 }}'

Also how do you write if you want the automation to happen every first Monday of September? How do you write that condition?

by the way, is “id:” irrelevant now? It’s not necessary anymore?

That would mean 20 is greater than or equal to the current day. In other words, the current day is less than 20.

You want 20 to be less than or equal to the current day.

Oh now that make sense. I understand it now. I thought it was a typo.

This condition checks if the all of the following are true:

  • the current month is the 9th month (September)
  • today is Monday
  • today’s date is less than 8
{{ now().month == 9 and now().weekday() == 0 and now().day < 8 }}

I’m testing the automation. But I’m not sure why it’s not looping. I double checked the time.

The script is exactly like how you wrote it too.

#Halloween Lights Theme
- alias: Halloween Lights Theme
  trigger:
    - platform: time
      at: '13:00:00'
  condition:
    - '{{ now().month == 10 and 20 <= now().day <= 31 }}'
  action:
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has started.
    - repeat:
        while:
          - condition: "(now().hour > 12 or now().hour < 18) and is_state('script.halloweenlights_s', 'on')"
        sequence:
          - service: script.halloweenlights_s
    - service: light.turn_off
      entity_id:
        - light.balcony_left
        - light.balcony_right
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has ended.

And also the iOS notification is not working?

The condition I had in while was incorrect. I have corrected the original example. This is how it should be:

   - repeat:
        while: "{{ now().hour > 16 or now().hour < 4 }}"

If you aren’t receiving the initial notification, it’s entirely possible the automation is failing to get past the initial condition and isn’t executing any part of the action.


EDIT

Relevant documentation: While Loop

Still a no go. This is what I have to test it.

#Halloween Lights Theme
- alias: Halloween Lighting
  trigger:
    - platform: time
      at: '13:30:00'
  condition: '{{ now().month == 10 and 20 <= now().day <= 31 }}'
  action:
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has started.
    - repeat:
        while: "{{ now().hour > 13 or now().hour < 4 }}"
        sequence:
          - service: script.halloweenlights_s
    - service: light.turn_off
      entity_id:
        - light.balcony_left
        - light.balcony_right
    - service: notify.ios_phones
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has ended.

and the script:

#Halloween Theme
halloweenlights_s:
  alias: Halloween Lights
  sequence:
    #sequence 1
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 2
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: darkgreen
      brightness_pct: '90'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: darkorchid
      brightness_pct: '90'
  - delay: '00:00:05'
    #sequence 3
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - delay: '00:00:05'
    #sequence 4
  - service: light.turn_on
    data:
      entity_id: light.balcony_left
      transition: '2'
      color_name: red
      brightness_pct: '25'
  - service: light.turn_on
    data:
      entity_id: light.balcony_right
      transition: '2'
      color_name: orange
      brightness_pct: '25'
  - delay: '00:00:05'

I think I have to re-integrate my ios devices, because I’m not getting the notification.

I also waited for it to run on its on at 15:50, but it didn’t run (just to see if the time will trigger the automation).

Test this automation:

#Halloween Lights Theme
- alias: Halloween Lighting Test
  trigger:
    - platform: time
      at: '17:15:00'
  condition: '{{ now().month == 10 and 20 <= now().day <= 31 }}'
  action:
    - service: persistent_notification.create
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has started.
    - repeat:
        while: "{{ now().hour > 13 and repeat.index < 5 }}"
        sequence:
          - service: persistent_notification.create
            data:
              title: '{{now().timestamp()|timestamp_local()}}'
              message: 'Loop: {{repeat.index}}'
          - delay: '00:00:05'
    - service: persistent_notification.create
      data:
        title: Home Automation Message
        message: Halloween Lights Theme has ended.

It will produce six persistent_notifications (visible in the Lovelace UI). I tried it and here are the notifications:

It confirms the Time Trigger, Template Condition, and looping all work. If it fails to work for you then something else is causing the problem.

I think there’s something wrong with my hassio. I think I’ll do a fresh install and redo this.

Before you go such lengths, confirm the time is set correctly. If the clock is set to the wrong time, all Time Triggers will be occurring not according to your local time but to some other time reference.

The time is correct too. This is what’s confusing me. What I’m thinking right now is to roll back to the older version. I keep older snapshots every time I do upgrades.

And along the way, I’ll get another Pi and work on configuring it with this Hassio with this newer coding technique and when all is working fine, I would switch the the old Hassio with the new one.

Did this problem start immediately after a recent upgrade?

Honestly, I never paid attention as I was focused on working with an Arduino project for around 6 months. I completely forgot that I wasn’t getting notifications on my phone anytime an automation triggers. So I can’t really remember when was the last time I received a notification.

And then last night, I realized that my lights should be be playing color sequences, but I noticed they never started. And so I checked the codes and I suspected the upgrade broke the automation. And I was right when I came and posted my issue here.

From my last snapshot, my last upgrade was in August – that was the 1.14 version. But I never paid attention to it after. The lights do turn on and off, so I never thought there was gonna be a problem with the looping scripts.

If the test automation I provided failed to produce the results I posted, this isn’t a problem with “looping scripts”.

If the automation failed to trigger at the designated time or did trigger but failed to generate the persistent_notifications, there is something fundamentally wrong here.

Yes. It didn’t show the results. And also the automation didn’t trigger on a specific time I set it at. So I think there’s something wrong with it.

I’ll try to get a Pi and work on this to transition my current Hassio to a newer version. Right now, I’m rolling it back to 107.6.

Anyway, thank you again @123, you’re very helpful on this. Once I got my hands on the Pi and encounter problems along the way, I’d post it here.