Declarative Template Switch Blueprint

This blueprint ties a single template definition e.g. is_state('sun.sun', 'above_horizon') to a set of switches or lights.

The switch/light will be triggered on and off depending on the template evaluation. You can add an optional delay when switching on, and a separate delay when turning off (useful when doing motion sensors for example).

The benefit of this blueprint is you can declaratively define when a light or switch should be on without wiring up any of the triggers yourself.

For simple template like is_state('sun.sun', 'above_horizon'), this blueprint is probably overkill but for more complex ones, it becomes a massive timesaver.

e.g. is_state('sun.sun', 'above_horizon') and states('sensor.battery_soc') | float > 95 and states('sensor.power_export_3min_average') | float > 0.5

By entering this into the blueprint, it will automatically wire up all the triggers and turn some switch or switches on when it is true. I’ve replaced dozens of tricky to get right automations with declarative ones with this blueprint.

template_to_switch_sync.yaml (github.com)

NOTE: It relies on installing GitHub - PiotrMachowski/Home-Assistant-custom-components-Custom-Templates: This integration adds possibility to use new functions in Home Assistant Jinja2 templating engine. to make ct_eval template function available since it uses this internally.

1 Like

Would this allow for syncing 2 switches, or no way to debounce / not get into a loop?