If sensor value is above 70% then binary sensor = on

Hi guys,

Been reading around a bit but cant seem to figure out how to do this.

I have a ‘sleep confidence’ sensor from my phone, I want to create a template binary sensor, where if the sleep confidence is over 70% then the binary sensor should change to on

If under 70% then it should be off…

Is anybody able to help me out with this ? :slight_smile:

Thank you !

Have to tried like this?

template:
  - binary_sensor:
      name: "Sleep Confidence State"
      state: >
        {% if states('sensor.sm_s908e_confidence') | int > 70 %}
          true
        {% else %}
          false
        {% endif %}

Or

{{ iif(states('sensor.sm_s908e_confidence') | int > 70, 'true', 'false') }}
3 Likes

Thanks both for your help!

We have a winner with:

- binary_sensor:
    name: "Lewys Sleep Confident"
    unique_id: 67297
    delay_on: 00:02:00
    delay_off: 00:00:05
    device_class: occupancy
    state: >
      {% if states('sensor.sm_s908e_sleep_confidence') | int > 70 %}
      true
      {% else %}
      false
      {% endif %}

Apologies - but, doesn’t int still need to have the default? int(0) ? (to prevent startup errors)

I think you can also set this up via the UI using the Threshold helper.

1 Like

Yes, that would be ideal.

1 Like

Hello,
I’ve got a similar problem. Maybe someone could help me :slight_smile:

I want do highlight my lovelace-icon in red oder grey depending on the state of my heating.
I tried using this template but it doesn’t work.

If the heating value is <0 ist should be red.
If the heating is 0 it should be grey.

type: horizontal-stack
cards:
  - type: custom:mushroom-template-card
    primary: Heizung
    secondary: '{{ states("sensor.durchschnittstemperatur") }}°C'
    icon: mdi:radiator
    icon_color: >
      {% if
      is_state('sensor.homematic_ip_accesspoint_heizkorperthermostat_wz_heating') | int < 0 %}
        red
      {% else %}
        grey
      {% endif %}
    layout: vertical
    tap_action:
      action: navigate
      navigation_path: heizung
    badge_icon: ''

This doesn’t work with one heating. But I also want do highlight the icon if every heating is >0.
How can I stack this?

Regards
Daniel