Create binary sensor based on entity states

Hey guys,

I wanted to have a binary sensor that basically shows me when I have excess power (when my battery is at more than 80% charge, the car is not charging and the solar panels produce energy.
So I basically try to check the state of three sensors and set another one on or off based off that.

This is what I came up with in my configuration.yaml:

- binary_sensor:
    - name: "excess_power"
      delay_on: "0:00:05"
      delay_off: "0:00:05"
      state: "{{ iif( states('sensor.powerwall_charge', with_unit=True, rounded=False) > 80  and states('sensor.tesla_wall_connector_state', with_unit=True, rounded=False) != 11 and states('sensor.powerwall_solar_now', with_unit=True, rounded=False) > 3, 'on', 'off', 'off')}}"

sadly this sensor is always shown as unavailable. Am I missing something here?

Best regards

I’m not sure why you are using “iif”, but apart from that what I would do to debug is start with 3 template sensors and then combine them. So the first 2 binary_sensors would be:

state: {{ states('sensor.powerwall_charge') | int > 80 }}

state: states('sensor.tesla_wall_connector_state') != '11'
or
state: states('sensor.tesla_wall_connector_state'|int) != 11

and take it from there? Maybe it’s just the != 11 (afaik states are always strings)?

1 Like

Easiest way to test these is in developer tools / templates.

1 Like

@PeteRage and @sj3fk3 , thank you so much for your help!

I did not actually know that you can test the expressions in the dev tools so I never got the error messages :frowning: - I feel so stupid now haha

It now works though with the following code:

- binary_sensor:
    - name: "excess_power"
      delay_on: "0:00:05"
      delay_off: "0:00:05"
      state: "{{ iif( states('sensor.powerwall_charge', with_unit=True, rounded=True |int > 80)  and states('sensor.tesla_wall_connector_state', with_unit=False, rounded=True | int != 11) and states('sensor.powerwall_solar_now', with_unit=False, rounded=True | int > 3), 'on', 'off', 'off')}}"

1 Like