Binary_Sensor change state from on/off to open/closed

Thanks, I’ve missed your reply!

This is the binary sensor (that works):

  - name: "Valvola PT"
    unique_id: "valvola_pt"
    state_topic: "home/centrale_termica/valvole/PT"
    value_template: "{{ value_json.value }}"
#    device_class: window
    payload_on: "1"
    payload_off: "0"

And based on the documentation, i’ve created this template:

 - binary_sensor:
    - name: 'Valvola pt template'
      state: >
      icon: >
        {% if is_state('binary_sensor.valvola_pt', 'on') %}
        {% else %}
        {% endif %}

But how can I use this new template? The binary sensor still has the default icon.

You created a new Template Binary Sensor called binary_sensor.valvola_pt_template and its icon is determined by the state of binary_sensor.valvola_pt.

binary_sensor.valvola_pt is an MQTT Binary Sensor and, according to its documentation, its icon can only be set to one image.

I’ve lost you.
I’ve discovered that the icon of a binary sensor can’t change when the state changes and when I’ve asked about the icon template you pointed me to the legacy and modern format, but maybe because I’m a newbie, I don’t know how to use the modern format.
The question is: how can I have an entity that changes the icon between valve-open to valve-closed based on a binary sensor?

Explanations have been already been provided (several times) in this topic. If they’re still unclear to you then I suggest you review the documentation (links posted above) while waiting until someone else volunteers to re-phrase the existing explanations.

That’s very kind of you!

I’ll hop on this discussion here. I have a diffuser integrated with localtuya. It has a binary_sensor that senses, if the water is empty. I’ve set the moist device class within localtuya. I included the water sensor state to a mini-humidifier card. Now I’m not happy with the shown states, that the different binary_sensor device_classes provide. Moist is near, but it’s neither dry or wet. I want to use something more like fill level related.
Is there a way on reassigning the binary_sensor states with a customization? I tried

$ cat customize.yaml
  value_template: |
    {% if states.binary_sensor.diffuser_needs_water_fault.state == 'off' %} ok
    {% elif states.binary_sensor.diffuser_needs_water_fault.state == 'on' %} needs water
    {% else %} unknown
    {% endif %}

On my first guess I’d say this not possible? Sounds like some circular dependency by the customization. If’ this is correct, is there a best practice?

Sure, I could just define a second binary_sensor that uses the diffusor values. I’m just curious and it feels a little more clean to rewrite the states.

Another approach would be to just use a value template/mapper for the for the mini humidifier indicator

No, binary sensors are purely on/off, and have device classes that alters how the UI shows that. You could do that with a standard template sensor though.

1 Like