Troubles with binary sensor (template)

I did set up two binary sensors, but something seems to be not ok and I can’t figure out what it might be. Even the sensors are shown, they are not working and the state is always defined as unavailable.

This is the binary_sensor.yaml file.

#### Binary sensors EV Charging ########
- platform: template
  sensors:
    # True if power is over 2000W
    ev_charging_onoff_2kw:
      friendly_name: 'EV is Charging with 2 kW'

      value_template: "{{states.sensor.sensor.shelly_em_channel_1_power|float < 2 and states.sensor.sensor.shelly_em_channel_1_power|float > 2000}}"
      device_class: battery_charging

    # True if power is over 3000W
    ev_charging_onoff_3_5kw:
      friendly_name: 'EV is Charging with 3.5 kW'
      entity_id:
        - sensor.shelly_em_channel_1_power
      value_template: "{{states.sensor.sensor.shelly_em_channel_1_power|float > 3000}}"

Try:

#### Binary sensors EV Charging ########
- platform: template
  sensors:
    # True if power is over 2000W
    ev_charging_onoff_2kw:
      friendly_name: 'EV is Charging with 2 kW'

      value_template: "{{((states.sensor.sensor.shelly_em_channel_1_power|float(0)) < 2) and ((states.sensor.sensor.shelly_em_channel_1_power|float(0)) > 2000)}}"
      device_class: battery_charging

    # True if power is over 3000W
    ev_charging_onoff_3_5kw:
      friendly_name: 'EV is Charging with 3.5 kW'
      entity_id:
        - sensor.shelly_em_channel_1_power
      value_template: "{{(states.sensor.sensor.shelly_em_channel_1_power|float(0)) > 3000}}"

Thanks, but it doesn’t change, still unavailable.

Use states.sensor.shelly_em_channel_1_power.state
Or better states("sensor.shelly_em_channel_1_power")

At first there was “sensor.sensor” und then the sensor was missing.

states("sensor.shelly_em_channel_1_power")
1 Like

Thanks for all the support(ers). It works now:

#### Binary sensors EV Charging ########
- platform: template
  sensors:
    # True if power is over 2000W
    ev_charging_onoff_2kw:
      friendly_name: 'EV is Charging with 2 kW'
      value_template: "{{((states.sensor.shelly_em_channel_1_power.state|float(0)) < 2) and ((states.sensor.shelly_em_channel_1_power.state|float(0)) > 2000)}}"
      device_class: battery_charging

   # True if power is over 3000W
    ev_charging_onoff_3_5kw:
      friendly_name: 'EV is Charging with 3.5 kW'
      value_template: "{{states.sensor.shelly_em_channel_1_power.state|float(0) > 3000}}"
      device_class: battery_charging
{{ ((states.sensor.shelly_em_channel_1_power.state|float(0)) < 2) and 
   ((states.sensor.shelly_em_channel_1_power.state|float(0)) > 2000) }}

I don’t see how that sensor’s value can ever be simultaneously less than 2 and greater than 2000.

Perhaps you want a logical OR instead of a logical AND?

1 Like

you are right it should be:

{{ ((states.sensor.shelly_em_channel_1_power.state|float(0)) > 1500) and 
   ((states.sensor.shelly_em_channel_1_power.state|float(0)) < 2500) }}

In that case, you can reduce it to this:

{{ 1500 < states('sensor.shelly_em_channel_1_power') | float(0) < 2500 }}