Timer Automations using Automations Editor

What is best practice to switch on / off devices using Automation Editor?

Currently I have two automations, one to switch on, the other to switch off a switch, but I would like to merge them into one automation. what are best practices to do this?

I don’t use the automation UI editor (I type them manually in YAML), but I don’t believe you can currently use templates in the automation editor, at least for actions (since you can’t use data_template, service_template, etc.) But if you post the YAML for both automations, we can probably help you create a single automation from that manually.

here you go :slight_smile:

  • id: ‘1574429754473’
    alias: Turn on Water Heater at 15:30
    description: Turn on Water Heater at 15:30
    trigger:
    • at: ‘15:30’
      platform: time
      condition:
      action:
    • device_id: 96dfe4e2a48d4444877872ea68ebba56
      domain: switch
      entity_id: switch.water_heater
      type: turn_on
  • id: ‘1574430324094’
    alias: Turn off Water Heater at 19:00
    description: Turn off Water Heater at 19:00
    trigger:
    • at: ‘19:00’
      platform: time
      condition:
      action:
    • device_id: 96dfe4e2a48d4444877872ea68ebba56
      domain: switch
      entity_id: switch.water_heater
      type: turn_off

Please format your code correctly, take a look at

I don’t know why you want to combine these, but I think it should work like this. There is probably a more elegant solution.

- id: ‘1574429754473’
  alias: "Turn on Water Heater at 15:30 and off at 19:00"
  description: "Turn on Water Heater at 15:30 and off at 19:00"
  trigger:
    - platform: time
      at: "15:30"
    - platform: time
      at: "19:00"
  action:
    service_template: >
      {% if {{now().strftime('%H:M') > strptime('18:59:00', '%T')}} %}
        switch.turn_off
      {% else %}
        switch.turn_on
      {% endif %} 
    entity_id: switch.water_heater

This will trigger the automation at 15:30 and at 19:30. It then checks if the current time is past 18:59. If it evaluates to True, the switch turns off, else it turns on.

1 Like

thanks!

which option i need to select to format my text properly?

Mark the code and press the button </> at the top or add ``` at the start and end of the code.

1 Like

Here’s another way to achieve the same result:

- id: '1574429754473'
  alias: "Turn on Water Heater at 15:30 and off at 19:00"
  description: "Turn on Water Heater at 15:30 and off at 19:00"
  trigger:
    - platform: time
      at: "15:30"
    - platform: time
      at: "19:00"
  action:
    service_template: "switch.turn_{{ 'on' if trigger.now.hour == 15 else 'off' }}"
    entity_id: switch.water_heater

When the automation runs at 15:30, trigger.now.hour will be 15 and the service_template will produce switch.turn_on.

When the automation runs at 19:00, trigger.now.hour will be 19 and the service_template will produce switch.turn_off.

2 Likes

this looks neat!

Glad to hear you like it.

As a courtesy to Burningstone, who was the first to respond with a solution, you may wish to consider removing the Solution tag from my post and put it back to BurningStone’s post. Only one post can be marked as the Solution and Burningstone offered it first.

2 Likes

Both these guys know their stuff and I would probably go with Taras’s solution (I have a completely unjustifiable bias towards templates), but for beginners Burning’s solution is clearer, easier to maintain and to extend/adapt to your other needs.
It’s completely up to you and how comfortable you are editing the raw files (very few regular contributors here use the AE though some (like Taras) are trying to tame that beast. Also expect rapid improvements in the AE soon to cover your stated issues - it’s a work in progress :rofl:

2 Likes

some people in here are really clever and helpful :slight_smile:

I have another question, maybe i get the answer because i think it is related.
I want to make an automation that sends the alarm panel status on change detected to a notification. the problem i am having how to put the status in the message part of the notification

Where are they.? We’ll hunt em down and kill them ! :crazy_face:

If the automation uses a State Trigger to monitor the alarm panel, then the alarm panel’s current state will be in trigger.to_state.state. See: Trigger State Object.

thanks but I am getting

2019-11-23 19:23:41 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.alarm_panel_change_send_notification. Error rendering template for call_service at pos 1: UndefinedError: ‘trigger’ is undefined

Show us your sample code please.

yes sure!
PS I am using the GUI to create this rule

- id: '1574523322809'
  alias: Alarm Panel Change send Notification
  description: Alarm Panel Change send Notification
  trigger:
  - entity_id: alarm_control_panel.alarm_panel
    platform: state
  condition: []
  action:
  - data:
      message: Alarm status changed to {{ trigger.alarm_control_panel.alarm_panel
        }}
    service: notify.hassio_notifier_s7

Change this:

- data:
    message: Alarm status changed to {{ trigger.alarm_control_panel.alarm_panel
        }}
  service: notify.hassio_notifier_s7

To this:

- data_template:
    message: Alarm status changed to {{ trigger.to_state.state }}
  service: notify.hassio_notifier_s7

same result :frowning:

I have this:

- id: '1574523322809'
  alias: Alarm Panel Change send Notification
  description: Alarm Panel Change send Notification
  trigger:
  - entity_id: alarm_control_panel.alarm_panel
    platform: state
  condition: []
  action:
  - data:
      message: Alarm status changed to {{ trigger.to_state.state }}
    service: notify.hassio_notifier_s7

and getting this in the log:
2019-11-23 20:33:19 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.alarm_panel_change_send_notification. Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

could it be the alarm panel does not support triggers?

We appear to now have two concurrent topics for this issue. The same question was asked in this thread, with the same reported errors, and the same suggestions for correcting it:

Petro and Burningstone explained you need to use data_template not data. Your latest example still shows you are using data.

Screenshot%20from%202019-11-23%2014-37-35

In addition, as explained by Tinkerer in the other topic, be sure not to trigger this automation manually. A manually-triggered automation skips the trigger and condition sections. As a result, when it gets to executing the action, the trigger object will be undefined and cause an error message.

1 Like

I missed that!!! I copied just the message part … sorry for that!!
Now worked fine!