Help on a template

I need help with this template. I can’t figure out how it makes the state of the binary_sensor change. The strange thing, to me, is that the icon changes correctly.
Did I do something wrong?

template:
- binary_sensor:
  - name: "Cancello Automobile"
    device_class: "moving" #"opening"
    state: >-    
        {% if is_state("binary_sensor.open_cancello_auto", "off")%}
          Chiuso
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          in movimento
        {% else %}
          Aperto
        {% endif %}
    icon: >-
        {% if is_state("binary_sensor.open_cancello_auto", "off") %}
          mdi:gate
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          mdi:gate-alert
        {% else %}
          mdi:gate-open
        {% endif %}

Grazie.

A binary_sensor can only be off or on. It cannot be Chiuso, in movimento, or Aperto

If you want those possibilities, you probably need sensor.

1 Like

In fact I need to have these written because I’m trying to group three entities into one to simplify the frontend.

then change “binary_sensor:” to “sensor:”.

So you cannot use a binary_sensor then. A binary sensor can only have 2 states (off and on).

A Sensor can have as many states as you like:

template:
- sensor:
  - name: "Cancello Automobile"
    device_class: "moving" #"opening"
    state: >-    
        {% if is_state("binary_sensor.open_cancello_auto", "off")%}
          Chiuso
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          in movimento
        {% else %}
          Aperto
        {% endif %}
    icon: >-
        {% if is_state("binary_sensor.open_cancello_auto", "off") %}
          mdi:gate
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          mdi:gate-alert
        {% else %}
          mdi:gate-open
        {% endif %}

Thanks for your help. Now works. Too bad you can’t change the color of the icon.
To make it work I had to delete the “device_class” line.

#template:
- sensor:
  - name: "Cancello Automobile"
    #device_class: "moving" #"opening"
    state: >-    
        {% if is_state("binary_sensor.open_cancello_auto", "off")%}
          Chiuso
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          in movimento
        {% else %}
          Aperto
        {% endif %}
    icon: >-
        {% if is_state("binary_sensor.open_cancello_auto", "off") %}
          mdi:gate
        {% elif is_state("binary_sensor.close_cancello_auto", "on") %}
          mdi:gate-alert
        {% else %}
          mdi:gate-open
        {% endif %}

You can change the colour of the icon in the dashboard.

Screenshot 2023-12-05 162618
On the dashboard I have these entities but the only one that does not change color is the “Car Gate sensor” that I just created with your help.
I really didn’t know how to do it.

is that an entities card?

I thin k the sensor needs to be on/off for the entities card to change colour.

You can set the colour yourself for any state if you used (for example) the (HACS) custom:button-card.

I had used “button_card” a while ago. I will try this project again.