Is something like automation action templating instead of multiple automations possible?

Hello everyone!

I am new to HA and trying to do one of my first automations: switching Day/Night theme based on sun. Automation must be triggered on restart too.

- alias: Night to Day Theme autoswitch
  hide_entity: true
  id: night_to_day_theme
  trigger:
  - platform: state
    entity_id: sun.sun
    from: 'below_horizon'
    to: 'above_horizon'
  - platform: homeassistant
    event: start
  condition:
    condition: state
    entity_id: sun.sun
    state: 'above_horizon'
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.hass_theme
      option: 'Solarized Light'
  - service: notify.telegram_group
    data:
      title: "Switching to *Day Theme*"
      message: "The current theme is *{{ states.input_select.hass_theme.state }}*"

I also want to send notifications only when the automation was triggered by the ‘sunset’ or ‘sunrise’ event, but not by homeassistant ‘start’ event.

It is just hard to believe it can not be done without having the automation split to two automations with a big chunk of code duplications like this:

- alias: Night to Day Theme autoswitch on Sun events with Notification
  hide_entity: true
  id: night_to_day_theme_notify
  trigger:
  - platform: state
    entity_id: sun.sun
    from: 'below_horizon'
    to: 'above_horizon'
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.hass_theme
      option: 'Solarized Light'
  - service: notify.telegram_group
    data:
      title: "Switching to *Day Theme*"
      message: "The current theme is *{{ states.input_select.hass_theme.state }}*"

- alias: Night to Day Theme autoswitch on Restart
  hide_entity: true
  id: night_to_day_theme
  trigger:
  - platform: homeassistant
    event: start
  condition:
    condition: state
    entity_id: sun.sun
    state: 'above_horizon'
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.hass_theme
      option: 'Solarized Light'

I’ve read about service_template and data_template, but not sure how/is possible to use them for achieving what I want. Is there any “dummy service call”, or some advanced templating possible? Please help.

I make a Home Assistant script called “do_nothing” that, well, does nothing. I use it when the templated logic should result in no service call being made.

It’s all quite tedious though, with any advanced automations. While Jinja Templates get the job done for simple stuff and more complicated stuff is possible, it’s not easy to write, or read. So, for anything beyond simple, I use AppDaemon to write automations giving me the full power of Python.

use the trigger platform as a condition in your automation action part?

            {{trigger.platform == 'sun' }}

don’t use that myself but would easily be added to this:

  - alias: 'Sun based theme change'
    id: 'Sun based theme change'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.sun_based_theme
    condition: []
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {{ states('sensor.sun_based_theme') }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.system
        data_template:
          title: >
            Ha Main: Sun set Theme
          message: >
            {{ as_timestamp(now()) | timestamp_custom("%X") }}: 
            Sun is {{states('sun.sun')}} and Frontend is set to '{{ states('sensor.sun_based_theme') }}'

btw, this is my sensor:

sensor:
  - platform: template
    sensors:
      sun_based_theme:
        friendly_name: Sun based theme
        value_template: >
          {% if is_state('sun.sun','above_horizon') %}
           {{states('input_select.set_sunrise_theme')}}
          {% else %}
            {{states('input_select.set_sunset_theme')}}
          {% endif %}
        icon_template: >
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

As @Mariusthvdb suggested, you can add a condition step that will cause the notification to only be sent when the automation was triggered by sun.sun changing state.

- alias: Night to Day Theme autoswitch
  hide_entity: true
  id: night_to_day_theme
  trigger:
  - platform: state
    entity_id: sun.sun
    to: 'above_horizon'
  - platform: homeassistant
    event: start
  condition:
    condition: state
    entity_id: sun.sun
    state: 'above_horizon'
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.hass_theme
      option: 'Solarized Light'
  - condition: template
    value_template: "{{ trigger.platform == 'state' }}"
  - service: notify.telegram_group
    data:
      title: "Switching to *Day Theme*"
      message: "The current theme is *{{ states.input_select.hass_theme.state }}*"

@Mariusthvdb, @pnbruckner Awesome! Thank you very much guys! I just was not aware of the possibility to use condition as a part of action. Just have found this also in the docs now: https://www.home-assistant.io/docs/automation/action/

Yes. The action part of an automation is a script.

The action part follows the script syntax which can be used to interact with anything via services or events.

If you click the link you’ll see everything you can do in a script, which is the same as what you can do in the action part of an automation.

1 Like

i’m kind of surprised that the action will work as expected since you are using a template in the “message:” but using “data:” instead of “data_template:” in the service options.

yep, good catch, that should definitely be changed. check in my post how that could be done: Is something like automation action templating instead of multiple automations possible?

or use this:

  - service: notify.telegram_group
    data_template:
      title: "Switching to *Day Theme*"
      message: >
        The current theme is *{{ states('input_select.hass_theme')}}*

Weird, but it actually works with data instead of data_template for some strange reason…

@drew-kun @finity @Mariusthvdb

Not weird or surprising. (Just not very well documented.)

The notify.XXX services accept a template for the message and title parameters. The only downside with letting the service render the template is that the trigger variable will not exist at that point. To use trigger you need to use data_template in the service call.

1 Like