New automation not loading (random switching of lights when away / on holiday)

I have several automations in my automations.yaml file, which all work fine. I have added an automation to switch on some lights in a small random window of time before sunset, and switch them off during a larger random window of time later in the evening, hoping this will fool possible criminals while I am away on holiday.
I have reloaded automations, I have rebooted, but the new automation does not load (is not at all visible in the frontend, not at configuration>automations, not at developer tools tools>states, all other automations are visible. I searched the web, this forum and more and do not know anymore. Your help is much appreciated.
The automation:

- id: '00007'
  alias: holiday lights
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:30:00'
    - platform: time
      at: '23:50:00'
  condition:
    condition: state
    entity_id: input_select.home_or_away
    state: 'holiday'
  action:
    delay:
      minutes: "{% if trigger.platform == 'sun' %}{{ (range(1, 25)|random|int) }}{% else %}0{{ (range(1, 120)|random|int) }}{% endif %}"
    service_template: >
      {% if trigger.platform == 'sun' %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: group.holiday_lights

Do a configuration check before you restart. This will point out errors like not having list hypens next to each action ā€œ-ā€

  action:
  - delay:
      minutes: "{% if trigger.platform == 'sun' %}{{ (range(1, 25)|random|int) }}{% else %}0{{ (range(1, 120)|random|int) }}{% endif %}"
  - service_template: >
      {% if trigger.platform == 'sun' %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: group.holiday_lights

Also, thatā€™s neat way of doing it!

Thanks for the help. I did use ā€˜configuration checkā€™ but that gave no errors. When I add the hyphens like you suggested, ā€˜configuration checkā€™ gives this error:
Error loading /config/configuration.yaml: while scanning for the next token
found character ā€˜%ā€™ that cannot start any token
in ā€œ/config/automations.yamlā€, line 93, column 8
Line 93 is the line with {% if trigger.platform == ā€˜sunā€™ %}.
BTW, your advise is spot on: as a relative HA newbie, I think I know now when to indent, but when to use a hyphen is stil a kind of mistery to me (it seems there is a lot of variation in online examples?).

Did you mean to put that 0 in the else case of your template?


{% if trigger.platform == 'sun' %}
  {{ (range(1, 25)|random|int) }}
{% else %}
  0{{ (range(1, 120)|random|int) }}
{% endif %}

No, that was a lost ā€˜0ā€™, from a previous version of the automation. I solved the ā€˜check configurationā€™ error by also adding an hyphen before entity_id: group.holiday_lights, and with an extra indent step for the if ā€¦ endif block, which I forgot. So the ā€˜check configurationā€™ error is gone, but the automation still does not load ā€¦

That does not sound right. Past the code as you have it now.

This is what I have now:

- id: '00007'
  alias: holiday lights
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:30:00'
    - platform: time
      at: '23:50:00'
  condition:
    condition: state
    entity_id: input_select.home_or_away
    state: 'holiday'
  action:
    - delay:
        minutes: "{% if trigger.platform == 'sun' %}{{ (range(1, 25)|random|int) }}{% else %}{{ (range(1, 120)|random|int) }}{% endif %}"
    - service_template: >
        {% if trigger.platform == 'sun' %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
    - entity_id: group.holiday_lights

alot of that is redundant.

The ()'s arenā€™t needed because the filters are only applied to the range() function.

The int isnā€™t needed because range(1, x) will return ints.

{{ range(1, 25) | random }}

Also, if you want to make that single line alittle tighter, you can use an ā€˜inlineā€™ if statement.

This:

{% if test %}
  {{ valuea }}
{% else %}
  {{ valueb }}
{% endif %}

can be transformed into this:

{{ valuea if test else valueb }}

And the beauty of an inline if statement like that is you can put it inside functions. Which will take your templates to short and concise solution:

action:
- delay:
    minutes: "{{ range(1, 25 if trigger.platform == 'sun' else 120) | random }}"
- service_template: "homeassistant.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
  entity_id: group.holiday_lights
2 Likes

Thanx! I can see the automation now. First I removed the () and int from the range function, but that did not seem to help, then I copied your final suggestion with all the compacted code (I did not know you can do that to code!), and now I can see the automation. Letā€™s see if this works this evening!

Came here to say the same thing. Itā€™s a clever workaround for the inability of templating the offset option.

Also came here to streamline the template but Petro already took care of that (plus I didnā€™t think of using the inline if within the range function; I had intended to use a variable).

Thanx to you too, Tom!

1 Like

After you tested and confirmed it works, be sure to mark the post with the Solution tag. This will automatically place a check-mark next to the topicā€™s title which signals to others that this topic has an accepted solution. It will also place a link beneath your first post that leads to the Solution post. All of this helps other users find answers to similar questions.

ā€¦ and done!

1 Like

Yesterday I confirmed the automation loaded and was visible in HA, last evening it all worked perfectly as expected. Here is the final version of the automation, for reference purposes:

- id: '00007'
  alias: holiday lights
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:30:00'
    - platform: time
      at: '23:55:00'
  condition:
    condition: state
    entity_id: input_select.home_or_away
    state: 'holiday'
  action:
    - delay:
        minutes: "{{ range(1, 25 if trigger.platform == 'sun' else 120) | random }}"
    - service_template: "homeassistant.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
      entity_id: group.holiday_lights

Glad itā€™s sorted, and thanks for posting the final version to help others looking for similar code.

The actual reason your last attempt didnā€™t work was the errant hyphen at the beginning of the last line, which @petro quietly fixed in the new code.