State changes

I’m hoping I’m in the right place :slight_smile: Sorry in advance if not.

What I’m trying to do and the reason I don’t think a search is finding it is becasue I believe my terminology may be wrong but here goes.

When you have a bed sensor for example it says open or closed. Trying to find an example of how to change the open to out of bed and closed to in bed.

Suggestions?

Huge Thanks!

You can only do this by creating a supplemental template sensor, and then display that template sensor in the frontend.

Which integration did you use to create the bed sensor?

And could you live with the states “Occupied” and “Clear”?

Thank you!!! I’ll do a search on that and learn something new!!

It’s nothing new, you did it here:

I was thinking if ir was a binary sensor defined in yaml you could apply the occupancy device class instead of creating a new sensor.

I could most def use that! I’d be happy with it. I used an aqara window sensor with a pressure mat.

Yes terminology hurts me most right now. I could have found that myself ugh… Thank you for pointing that out!!!

OMG I already built that… ughhhhhhh it’s in my sensor dir…

- platform: template
  sensors:
    bed_sensor_contact: 
      value_template: >- 
        {% if is_state('binary_sensor.bed_sensor_contact2', 'closed') %}
          In Bed
        {% else %}
          Out of bed
        {% endif %}   

Just need to modify it a bit… Sorry for wasting everyone’s time. Thank you!

Interesting or I’m missing something really simple but I get no change from this…it still says Open

- platform: template
  sensors:
    bed_sensor_contact:
      value_template: >-
        {% if states('binary_sensor.bed_sensor_contact_2', 'Open') %}
          Out of Bed
        {% else %}
          In bed
        {% endif %}

I’ve tried ‘open’ as well… no changes.

Binary sensors are on or off

Three ways, pick the one:

- platform: template
  sensors:
    bed_sensor_contact:
      value_template: >-
        {% if is_state('binary_sensor.bed_sensor_contact_2', 'on') %}
          Out of Bed
        {% else %}
          In bed
        {% endif %}
- platform: template
  sensors:
    bed_sensor_contact:
      value_template: "{{ 'Out of Bed' if is_state('binary_sensor.bed_sensor_contact_2', 'on') else 'In Bed' }}"
- platform: template
  sensors:
    bed_sensor_contact:
      value_template: "{{ iif( is_state('binary_sensor.bed_sensor_contact_2', 'on'), 'Out of Bed', 'In Bed', 'Unknown') }}"

I hear what you’re saying but this is a door/window sensor and it’s showing Open and Closed …

So bit confused… it should still change the state shouldn’t it?

Look in developer tools / sates. That is the best place to see what the actual state of your entity is.

Lovelace can change the display of binary sensors from states on or off and display them as other formats depending on what device class is applied. This does not change the underlying state that is always on or off though. Look at all the display possibilities for the on or off states here:

Binary Sensor Device Classes

That was what I was on about before. You could apply the device_class occupancy to your bed sensor and Lovelace would show Occupied or Clear and this would not require an additional template sensor.

You can apply any device class to your bed binary sensor using customize.

homeassistant:
  customize:
    binary_sensor.bed_sensor_contact_2:
      device_class: occupancy
1 Like

Added the customize to customize.yaml - Developer tools says ‘on’ my HA screen says Detected…since it’s now an occupancy device but nothing is changing what I want to show on the screen… which is in bed or out of bed…

This is what the Developer Tools state shows:
bed

Since I changed the device type to occupancy this is what my screen shows:

bed1

I also have this:

- platform: template
  sensors:
    bed_sensor_contact:
      value_template: >-
        {% if is_state('binary_sensor.bed_sensor_contact_2', 'on') %}
          Out of Bed
        {% else %}
          In bed
        {% endif %}

So it looks like I have too many wires crossed and it’s just not working…

Did you restart home assistant after making these changes?

Do a configuration check first.

Yes added, always check the config [hard lesson learned there once]… restarted.

It showed Detected instead of Open…

Ok so the device class is working. It’s just that the documentation is incorrect. :roll_eyes: The text says “occupied” but the image shows “detected”. I’ll update the docs.

Undo the the device class additions and go with petro’s solution.

1 Like

It needs to be in sensor section of your config, not binary sensor. Binary sensors can only be on or off. Which is not the states you want.

1 Like

Yes I have this in my config… not actual sensor section but a seperate file

sensor: !include_dir_merge_list includes/sensors/

And I have put this called bed.yaml in the sensor dir

- platform: template
  sensors:
    bed_sensor_contact_2:
      value_template: >-
        {% if is_state('binary_sensor.bed_sensor_contact_2', 'on') %}
          Out of Bed
        {% else %}
          In bed
        {% endif %}

And I still get 'Open" on the frontend … so not sure what’s going on. The Developer Tools says ‘on’ so I used that… so in theory it should be working… it’s just not.