Change binary_sensor's states in lovelace card

Hi there,

I have a binary_sensor indicating if an update is available to a lightbulb or not. On my frontend it’s displayed as either “on” or “off”, as binary sensors do, which is not intuitive regarding the availability of an update.

I’m trying to customize it to something like “yes/no” or “available/not available”. I know of device classes, but none seems to display anything like that. Also even if some did, it might not do so next time, so I thought I’d asked for a general way of customzing the states.

Thanks,

Martin

Hi there, there are multiple methods to achieve this, You can use custom components like custom button card to do this in lovelace. There are also other custom lovelace integrations that make this possible.

But instead of that we can create a template sensor in home assistant which will show available as state when the binary sensor is on and unavailable otherwise. This template sensor should look like this.

sensor:
  - platform: template
    sensors:
     light_update:
        friendly_name: 'Light Update Status'
        value_template: >
          {% if states.binary_sensor.light_update == 'on' %}
            Available
          {% elif %}
            Unavailable
          {% endif %}

Please make sure you change the binary_sensor entity id to yours. If you tell me how you operate this binary sensor, we may even be able to remove it by making the template accordingly.

Thank you,

adding a sensor works. But since I have a couple of lights, I have to add sensor for each light, right? That’s why I’d hoped I could just overwrite the value somehow with lovelace.

I’ll look into custom button cards.

Thanks!

Btw: regarding removing the binary_sensor, I suppose I’d just write whatever sets the binary sensor as a condition in the value template of the sensor instead of refereing to the binary sensors state. But in this case it is not an option since the binary sensors are entities from zigbee2mqtt.

No you dont have to do that, You can incooperate both lights into a single template sensor.

sensor:
  - platform: template
    sensors:
     light_update:
        friendly_name: 'Light Update Status'
        value_template: >
          {% if states.binary_sensor.light1 == 'on'  and states.binary_sensor.light2 == 'on'  %}
            Update Available For Both Lights
          {% elif states.binary_sensor.light1 == 'on'  and states.binary_sensor.light2 == 'off' %}
            Update Available For Light1
          {% elif states.binary_sensor.light1 == 'off'  and states.binary_sensor.light2 == 'on' %}
            Update Available For Light2
          {% else %}
            Unavailable
          {% endif %}
1 Like

Ok, with two bulbs thats feasible, but with 7 or 8 it would’t practically work this way, since there would be about 200 to 300 combinations I would have to account for. Or am I missing something?

In that case the best thing would be to use nodered which can compile all these into a single payload for the sensor in any combination by using the function node.

I would like to do this but i get “TemplateSyntaxError: Expected an expression, got ‘end of statement block’” running this with my binary sensor?

Where do i go wrong?

Try this. Instead of else, i used elif earlier. This should work

sensor:
  - platform: template
    sensors:
     light_update:
        friendly_name: 'Light Update Status'
        value_template: >
          {% if states.binary_sensor.light1 == 'on'  and states.binary_sensor.light2 == 'on'  %}
            Update Available For Both Lights
          {% elif states.binary_sensor.light1 == 'on'  and states.binary_sensor.light2 == 'off' %}
            Update Available For Light1
          {% elif states.binary_sensor.light1 == 'off'  and states.binary_sensor.light2 == 'on' %}
            Update Available For Light2
          {% else %}
            Unavailable
          {% endif %}

that whole template has syntax errors.

  1. Always use states() method when you can and avoid state objects. i.e. states('binary_sensor.light1') instead of states.binary_sensor.light1.state.
  2. if you’re going to use state objects, use them properly. I.e. if you want the state, you have to express that when you access it. states.binary_sensor.light1.state, not states.binary_sensor.light1
  3. If you’re checking an on/off state, use is_state instead of states() or the states object. i.e. is_state('binary_sensor.light1', 'on')
  4. if you have multiple lights (more than 2) this template will get cumbersome to deal with. Just make a list of lights and filter out what’s off.

@Izze

sensor:
  - platform: template
    sensors:
     light_update:
        friendly_name: 'Light Update Status'
        value_template: >
          {% set lights = 'binary_sensor.light1', 'binary_sensor.light2' %}
          {% set updates = expand(lights) | selectattr('state','eq','on') | map(attribute='name') | list %}
          {% if updates %}
            {{ updates | join(', ') }}
          {% else %}
            No Updates
          {% endif %}

to add lights, simply add: , 'binary_sensor.xyz' in the set lights line.

1 Like