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