I’m trying to switch an input boolean on or off based on a template value.
I can do it using an if in my automation, but wondered if there was a more compact way of doing this:-
service-template: >
input_boolean.turn_{%if state_attr('weather.forecast_home_hourly','temperature')<6%}
on
{%else%}
off
{%endif%}
entity-id: input_boolean.winter_mode_no_open_window_heat_shutoff
(which doesn’t work - says cannot determine action)
TL;DR (although seriously, read both of the linked docs) - {{ if the template that follows results in a value, {% if it does not.
Yes, there is. Don’t use an automation or an input boolean, and this template sensor to your configuration.yaml instead:
template:
- binary_sensor:
- name: Winter Mode No Open Window Heat Shutoff
state: "{{ state_attr('weather.forecast_home_hourly','temperature')<6 }}"
HA will ensure the state of that binary sensor always matches the output of that template. And you can just use that binary sensor around the rest of HA like you were planning to use the input boolean.
This is assuming you never intend to modify the input boolean manually in the UI. If you want to be able to do that then follow the instructions in the posts above for shrinking the automation. Or it can be made into a trigger template select but that’s probably not more compact anymore.
Thanks everyone for their support. The community here is amazing.
The original use case of the boolean was a manual switch that I controlled in the UI, that I then started to considered automating. I’m not sure if I want to keep some element of manual control over it, but @CentralCommand 's suggestion is interesting.