Skip to content

Instantly share code, notes, and snippets.

@megakid
Last active April 16, 2023 23:14
Show Gist options
  • Save megakid/7b395af9db697a9e8c773fb14ec74f27 to your computer and use it in GitHub Desktop.
Save megakid/7b395af9db697a9e8c773fb14ec74f27 to your computer and use it in GitHub Desktop.
blueprint:
name: Sync template to switch
description: Turn on a switch when template is true
domain: automation
input:
template_string:
name: Template string
description: Do not include {{ }} or {% %} - just a single expression that will evaluate to true/false
default: "<some template here e.g. is_state('binary_sensor.abc', 'on')>"
selector:
template:
switch_entities:
name: Entities
selector:
target:
entity:
domain:
- switch
- light
- input_boolean
on_delay:
name: (Optional) How long the template must be 'true' before switching
default: 0:00:00
selector:
duration:
off_delay:
name: (Optional) How long the template must be 'false' before switching
default: 0:00:00
selector:
duration:
trigger_at_homeassistant_startup:
name: (Optional) Trigger at Home Assistant startup
description: Trigger the target state check and enforcement at Home Assistant startup.
default: false
selector:
boolean:
source_url: https://gist.github.com/megakid/7b395af9db697a9e8c773fb14ec74f27
trigger_variables:
templatestr: !input template_string
trigger:
- platform: homeassistant
event: start
- platform: template
for: !input on_delay
value_template: |-
{% set t = '{{ ' + templatestr + ' }}' %}
{{ ct_eval(t) }}
- platform: template
for: !input off_delay
value_template: |-
{% set t = '{{ ' + templatestr + ' }}' %}
{{ ct_eval('{{ not bool("' + (ct_eval(t) | string) + '") }}') }}
#{% set t = '{{ ' + templatestr + ' }}' %}
variables:
trigger_at_homeassistant_startup: !input trigger_at_homeassistant_startup
condition:
- condition: template
value_template: '{{ trigger.platform!="homeassistant" or trigger_at_homeassistant_startup }}'
action:
- if:
- condition: template
value_template: |-
{{ ct_eval("{{ " + templatestr + " }}") }}
then:
- service: homeassistant.turn_on
target: !input switch_entities
else:
- service: homeassistant.turn_off
target: !input switch_entities
mode: restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment