Binary sensor templating - how to set boolean value

Hi all.

I need help :slight_smile:
After countless attempts I manage to set a value of binary_sensor via template. But I have to use quite nasty trick. And there must be better way.

This is the sensor:

platform: template
sensors:
  workday_status:
    friendly_name: Workday status
    entity_id:
      - binary_sensor.workday
      - input_boolean.force_workday
      - input_boolean.force_freeday
    value_template: >-
      {% if is_state('input_boolean.force_workday', 'off') and is_state('input_boolean.force_freeday', 'off')  %}
        {{ states('binary_sensor.workday') }}
      {% elif is_state('input_boolean.force_workday', 'on') and is_state('input_boolean.force_freeday', 'on')  %}
        {{ states('binary_sensor.workday') }}
      {% elif is_state('input_boolean.force_workday', 'on')  %}
        {{ 1 < 2  }}
      {% elif is_state('input_boolean.force_freeday', 'on')  %}
        {{ 1 > 2  }}
      {% else %}
        {{ states('binary_sensor.workday') }}
      {% endif %}

How to replace these ugly lines?
{{ 1 > 2 }}
{{ 1 < 2 }}

I tried on / off , true / false, β€˜on’ / β€˜off’, capital letters, whatever. Nothing worked and the sensor did not get updated.
The solution above is working as it makes evaluation and returns true boolean value.

Thanks.

1 Like
      {% elif is_state('input_boolean.force_workday', 'on')  %}
        {{ false }}
      {% elif is_state('input_boolean.force_freeday', 'on')  %}
        {{ true }}
      {% else %}

Thanks @tom_l

It works. So still evaluation in place but a lot smarter :slight_smile: