Templating an input_boolean (from off/on to not home/home)

Need to change the state of an input_boolean from off to not home and from on to home.

Any help, thanks!

You could exchange it with an input_select with the two options? :slight_smile:

1 Like

I am doing something similar with a device tracker, butcher it for what you want
‘’’

  • platform: template
    sensors:
    geertphone:
    friendly_name: “Geert’s tel.”
    value_template: “{% if is_state(‘device_tracker.geert_phone’, ‘home’) %}online{% else %}offline{% endif %}”

‘’’
I change sensor to home and offline based on value of the state of a device_tracker.

1 Like

Make a template binary sensor using device class presence. On = home, off = not_home. Use a value template that grabs the state of the input boolean.

input_boolean:
  home_not_home_toggle:
    name: Am I home
    initial: off
binary_sensor:
  - platform: template
    sensors:
      am_i_home:
        friendly_name: Manual Home Binary Sensor
        value_template: "{{ states('input_boolean.home_not_home_toggle') }}"
        device_class: presence

You’ll end up with binary_sensor.am_i_home. Then you can use that for automations. checking the states as home/not_home.

3 Likes

Thank you all

Tried your solution but its not working, very strange. The template its working poerfectly: when the input_boolean go to on the template says on, but the binary_sensor always stays to off …???

binary_sensor:
  - platform: template
    sensors:
      john_phone_home:
        friendly_name: John phone home
        value_template: "{{ states('input_boolean.john_home_occupancy') }}"
        device_class: presence

input_boolean:
  john_home_occupancy:
    name: John home
    initial: off

Also this gives me true

{{ is_state(‘input_boolean.john_home_occupancy’, ‘on’) }}

but the binary_sensor.john_phone_home is off

what am I missing here?

binary_sensor:
  - platform: template
    sensors:
      john_phone_home:
        friendly_name: John phone home
        value_template: >-
          {{ is_state('input_boolean.john_home_occupancy', 'on') }}

Also this not working (tried sensor)

sensor:
  - platform: template
    sensors:
      john_phone_home:
        friendly_name: John phone home
        value_template: >-
          {% if is_state('input_boolean.john_home_occupancy','on') %}
          Home
          {% elif is_state('input_boolean.john_home_occupancy','off') %}
          Away
          {% else %}
          Unknown
          {% endif %}

can you get the state of your input boolean inside the template editor?

Yes! And it’s correct… I don’t get it

In front end I get this for the sensor.john

Home # True # “on”

{{ is_state(‘input_boolean.john_home_occupancy’, ‘on’) }}

Gives True

even when you turn the boolean off?

With off it gives False

{{ is_state(‘input_boolean.john_home_occupancy’, ‘on’) }}

ok, so are you sure you are watching the correct sensor then? If it template properly switches, then the sensor should as well. If not, you are doing something else wrong, possibly where you are placing it in the config.

I think my problem is, also, in my automation, could you please help?
sensor.john_home_occupancy_confidence it is unit of measurement %, so maybe that’s a problem?

When this sensor goes from unknown to 100 (%), the automation is not fired, the input_boolean.john_home_occupancy stays off)

  - alias: John Occupancy On
    hide_entity: true
    trigger:
      - platform: numeric_state
        entity_id: sensor.john_home_occupancy_confidence
        above: 10 
    action:
      - service: homeassistant.turn_on
        data:
          entity_id: input_boolean.john_home_occupancy

Maybe this?
(sensor.john_home_occupancy_confidence is a mix_max sensor, so it has these attributes

count_sensors: 3
max_value: 100
mean: 100
min_value: 100
last: 100
unit_of_measurement: %
friendly_name: John Home Occupancy Confidence
icon: mdi:calculator

  - alias: John Occupancy On
    hide_entity: true
    trigger:
      - platform: numeric_state
        entity_id: sensor.john_home_occupancy_confidence
        value_template: '{{ state.attributes.last | int }}'
        above: 10 
    action:
      - service: homeassistant.turn_on
        data:
          entity_id: input_boolean.john_home_occupancy

You shouldn’t have unknown as a value. Numeric_states only work with numbers, so non-number to non-number will not work.

what sensor is the min_max sensor based off of?

All three mqtt sensors are the same as the one shown: the values can be any number between 0 and 100 and unfortunately sometimes can be Unknown

  - platform: min_max
    name: "John Home Occupancy Confidence"
    type: max
    round_digits: 0
    entity_ids:
      - sensor.john_office_presence
      - sensor.john_guest_room_presence
      - sensor.john_living_room_presence

  - platform: mqtt
    state_topic: 'monitor/pigames/john_phone'
    value_template: '{{ value_json.confidence }}'
    unit_of_measurement: '%'
    name: 'John living room presence'

Why does the min-max sensor go unknown then? Is that on startup?

problem solved: service has to be input_boolean.turn_on