How to merge two simple on off automations into one

Hi I want to combine two automation into one, using templates. I’m completely new to templates, so I have no idea what I’m doing^^
Normally I make two automation to switch something on and off.

This is my Automation. Why its not working? Thx a lot.

- id: Weihnachtsstern 
  alias: Weihnachtsstern
  trigger:
    platform: sun
    event: sunset
    offset: "-01:00:00"
    platform: state
    entity_id: sensor.time
    state: "20:00"
  action:
    service_template: >
      {% if trigger.event == "sunset" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "20:00" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.weihnachtsstern

Solution thx @keithh666

I think your list of triggers needs "- platform: " for each platform statement.

Depending on which trigger triggers the action one of the triggers won’t be valid I think - however I could be wrong :P, I’m not really sure how HA processes the triggers in a template, maybe someone more experienced in templates can comment.

Thx for reply.

I have a similar automation which is working with only one trigger triggers :smiley: so I hope its working. But I dont know too.

The config checker show also “Configuration invalid”

- id: Weihnachtsstern 
  alias: Weihnachtsstern
  trigger:
    - platform: sun
      event: sunset
      offset: "-01:00:00"
    - platform: state
      entity_id: sensor.time
      state: "20:00"
  action:
    service_template: >
      {% if trigger.event == "sunset" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "20:00" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.weihnachtsstern
- platform: state
  entity_id: sensor.time
  state: "20:00"

Should be…

  - platform: time
    at: "20:00"

thx

I changed the trigger to time and the template to trigger.now

The configuration is valid now.
Now I have to test if its working.

- id: Weihnachtsstern 
  alias: Weihnachtsstern
  trigger:
    - platform: sun
      event: sunset
      offset: "-01:00:00"
    - platform: time
      at: "20:00"
  action:
    service_template: >
      {% if trigger.event == "sunset" %}
      homeassistant.turn_on
      {% elif trigger.now == "20:00" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.weihnachtsstern

Ok I changed the time to 20:50

The automation has been triggered but the switch did not switched off. So I think something is wrong with the turn_off action.

trigger is not an entity, the template does not know about what trigger is or what was the thing that triggered it. So you would basically have to do the same sun logic within your action if you want them combined.

Try using the dev tool: Templates and toss in your template.
{{trigger.event}} will throw an undefined error.

Pretty sure keeping them separate would be way more readable and less complicated.

trigger.event is available when using the sun platform and automation templating, per the docs.

Trigger state objects are tough to debug though, because you can’t evaluate them in the templates dev tool (because there is no trigger in that case!).

@kolossboss did anything show up in the error log on info page of the front end under developer tools when the switch failed to turn on?

Can’t this be changed to {% else %}

2 Likes

Cool good to know! Thanks!

@azeroth12
Yes I could keep them separate. But I have many similar automations like this and my whole automations.yaml becomes bigger and bigger :smiley:

@Dolores
I got this in the log:
2017-11-29 20:50:00 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service:

@keithh666
I can try this. Thx.

wow that works thx a lot :smiley:
It that simple!! ^^

- id: Weihnachtsstern 
  alias: Weihnachtsstern
  trigger:
    - platform: sun
      event: sunset
      offset: "-01:00:00"
    - platform: time
      at: "16:08"
  action:
    service_template: >
      {% if trigger.event == "sunset" %}
      homeassistant.turn_on
      {% else %}
      homeassistant.turn_off
      {% endif %}
    entity_id: switch.weihnachtsstern
2 Likes

@kolossboss you might consider renaming the thread title to something more descriptive.

1 Like