Do I need a binary sensor to do this?

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)

service_template was deprecated nearly 2 years ago…

service: input_boolean.turn_{{ iif( state_attr('weather.forecast_home_hourly','temperature') < 6, 'on', 'off') }}
target:
  entity_id: input_boolean.winter_mode_no_open_window_heat_shutoff

Also don’t forget to change this:

entity-id:

To this:

entity_id:
1 Like

The joys of digging up old forum posts it seems. Thanks for your help once again.

One last question. Where would I find more reading about when to use {% Vs {{ ?

And in Drew’s reply he puts iif, is that a typo or intended?

No it’s not a typo.

https://jinja.palletsprojects.com/en/3.1.x/templates/#synopsis

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.

1 Like

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.

Thanks again.

1 Like