Entity button with icon state on value of sensor on/off

No problem … thanks finity for your intrest :blush:

Then the first is an entity (sensor).
The entity check that the status of the relay
here in the off position
Off

the second is an entity button.
This change the relay state via modbus
here in the on position
On

As you can see, the button icon is always off even if the lamp is on.
That is, it turns on, rightly, for a moment and then turns off every time I press it. So if I didn’t have the other entity (sensor) I wouldn’t understand when the lamp is on or not

My intent would be to have only one entity button that shows its on / off icon based on the state of the entity (sensor).

I hope I was clearer

Ok.

I think you will need to create a template switch that tells the actual switch to turn on & off as the action and then use the state of the relay as the state of the template switch.

Then you can use the template switch instead of your actual switch and then everything should work the way you want.

Hi finity,
I added the template switch.
I have also set an icon different from the default one on the entity button but, it doesn’t change my icon and not as before
The light dont work !!
Where am I wrong? :sleepy:

switch:
  - platform: mqtt
    name: "Basic sonda"
    state_topic: "stat/basic_sonda/POWER"
    command_topic: cmnd/basic_sonda/POWER
    availability_topic: "tele/basic_sonda/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: mqtt
    name: "Basic 1"
    state_topic: "stat/basic1/POWER"
    command_topic: cmnd/basic1/POWER
    payload_on: "ON"
    payload_off: "OFF"

  - platform: template
    switches:
      androne:
        friendly_name: "Androne"
        value_template: "{{ is_state_attr('binary_sensor.stato_ingresso_androne', 'sensor_state', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        turn_off:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        icon_template: >-
          {% if is_state('binary_sensor.stato_ingresso_androne', 'sensor_state', 'on') %}
            mdi:lightbulb-on-outline
          {% else %}
            mdi:lightbulb-outline
          {% endif %}

# Accensioni controllino
  - platform: modbus
    scan_interval: 1
    registers:
    - name: rele_ingresso_androme
      hub: maxi
      slave: 1
      register: 1
      command_on: 32
      command_off: 0

binary_sensor:
  # Stato accensioni controllino      
  - platform: modbus
    scan_interval: 1
    coils:
      - name: stato_ingresso_androne
        hub: maxi
        slave: 1
        coil: 5

In entity switch I have put this code:

entity: switch.androne
hold_action:
  action: more-info
show_icon: true
show_name: true
tap_action:
  action: toggle
type: entity-button

this should use is_state_attr

also, and pardon me if I missed this in the discussion before, but you use service: switch.toggle for turning on and off. Why not use switch.turn_off and switch.turn_on

if the only thing you need is to set an icon and icon_color based on the state of a sensor, you best use the customization I posted before. You are making things very complicated right now. (but again, sorry if I missed the essentials of needing a dedicated template switch. I thought you had that part working already before you started this topic…)

I think the problem is that the switch was a momentary switch that when the button was pushed only stayed on for a second then went back off immediately. Then the relay was operated to turn on the light. All of that was operating correctly in the HA frontend.

The OP wanted a button that would show a status that followed the state of the relay, not the state of the momentary button.

1 Like

exactly finity.
In two words you said hundreds of my words :open_mouth:

I see, that is nothing very special. Having a frontend entity display the state of another entity.
For me the question remains, why not use the customization with the state of that relay? Icon and icon color would easily be templated in custom-ui

Mariusthvdb and finity,
thank you so much for the passion you put into making me understand what to do.
I’m a newbie to HA and unfortunately I can’t translate what you suggested into code.

sorry :innocent:

as posted earlier you can customize the icon. Ill add the technique of doing so with another entity (as that is what you need)

sensor.your_sensor:
  templates:
    icon: >
      if (entities['sensor.other_sensor'].state === 'on') return 'mdi:laptop';
      return 'mdi:laptop-off';
    icon_color: >
      if (entities['sensor.other_sensor'].state === 'on') return 'rgb(251, 210, 41)'; # or color name eg yellow
      return 'rgb(128, 128, 128)';

All of that is true but it also entails the extra (not unsubstantial) step of installing and maintaining another custom component and it also means learning a new templating format language that a new person might not want to undertake.

My way is supported natively in the code so it’s a bit more straight forward.

2 Likes

try this:

- platform: template
    switches:
      androne:
        friendly_name: "Androne"
        value_template: "{{ is_state('binary_sensor.stato_ingresso_androne', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        turn_off:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        icon_template: >-
          {% if is_state('binary_sensor.stato_ingresso_androne', 'on') %}
            mdi:lightbulb-on-outline
          {% else %}
            mdi:lightbulb-outline
          {% endif %}

Hi, Sure you’r right, I am not disputing that at all, and I think the template switch is a very nice tool in the box. So great to show in this case and probably the solution for the OP’s technical issue.

I know my suggestion needs the custom-ui install. This is a 1 time thing though, and provides a plethora of options throughout the full setup though, so certainly worth the effort.

Adding to that, there’s no native way to color icons, besides the yellow and black used now, (and your suggestion above doesn’t color the icons either ) other than installing extra custom-cards in Lovelace. Which, as it stands now, requires much more effort and has a steep learning curve, since many of these cards use their own way of templating, and have their own level of support for templating all together. Not to mention the efforts one has to take to update these and change when features require so after updating. In fact, keeping custom cards up to date and keep up with breaking changes in Lovelace is a task by itself…:wink:

Coming to think of it, Themes could of course also color the individual items but is a global way of changing the frontend, while custom-ui enabled individual customizing.

Hi finity,
I tried to insert the code you attached to me but it doesn’t work.
Practically when I press the entity button it does not turn on or off the light and does not change the icon.
I think I’ll have to wait if an entity switch is ever added with a status input for the icon.
Thanks anyway, Alberto

switch:
  - platform: mqtt
    name: "Basic sonda"
    state_topic: "stat/basic_sonda/POWER"
    command_topic: cmnd/basic_sonda/POWER
    availability_topic: "tele/basic_sonda/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: mqtt
    name: "Basic 1"
    state_topic: "stat/basic1/POWER"
    command_topic: cmnd/basic1/POWER
    payload_on: "ON"
    payload_off: "OFF"

  - platform: template
    switches:
      androne:
        friendly_name: "Androne"
        value_template: "{{ is_state('binary_sensor.stato_ingresso_androne', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        turn_off:
          service: switch.toggle
          data:
            entity_id: switch.rele_ingresso_androne
        icon_template: >-
          {% if is_state('binary_sensor.stato_ingresso_androne', 'on') %}
            mdi:lightbulb-on-outline
          {% else %}
            mdi:lightbulb-outline
          {% endif %}
          
# Accensioni controllino
  - platform: modbus
    scan_interval: 1
    registers:
    - name: rele_ingresso_androme
      hub: maxi
      slave: 1
      register: 1
      command_on: 32
      command_off: 0

binary_sensor:
  # Stato accensioni controllino      
  - platform: modbus
    scan_interval: 1
    coils:
      - name: stato_ingresso_androne
        hub: maxi
        slave: 1
        coil: 5      

sensor:
  # Weather prediction
  - platform: yr
  # Mosquitto
  - platform: mqtt
    name: Sonda
    state_topic: "tele/basic/SENSOR"
    unit_of_measurement: '°C'
    value_template: '{{ value_json.DS18B20.Temperature }}'
  - platform: command_line
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ value | multiply(0.001) | round(1) }}'
  # PC Status  
  - platform: systemmonitor
    resources:
      - type: disk_use_percent
        arg: /home
      - type: memory_free
      - type: memory_use_percent
      - type: network_in
        arg: enp1s0
      - type: network_out
        arg: enp1s0
      - type: throughput_network_in
        arg: enp1s0
      - type: throughput_network_out
        arg: enp1s0

wait wait wait.
IT WORKS !!!
The braces were missing

  - platform: template
    switches:
      androne:
        friendly_name: "Androne"
        value_template: "{{ is_state('binary_sensor.stato_ingresso_androne', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            {"entity_id": "switch.rele_ingresso_androme"}
        turn_off:
          service: switch.toggle
          data:
            {"entity_id": "switch.rele_ingresso_androme"}
        icon_template: >-
          {% if is_state('binary_sensor.stato_ingresso_androne', 'on') %}
            mdi:lightbulb-on-outline
          {% else %}
            mdi:lightbulb-outline
          {% endif %}

INFINITY Thanks finity :heart_eyes:

1 Like

I’m glad it’s working but I have no idea why it needed those brackets. I’ve never seen the use of those brackets like that in that situation.

I don’t know, I just went to the services page and either switched on the light with the service.toggle and copied the service data !!

However it works !! :laughing:

1 Like

Your solution works, I try it both with and without the extra “brackets” (that @alverman used). Not sure why he need those extras…Thanks for your solution.

Okay my issue is related…

So I’ve set up a sensor that changes Icon based on state:

  - platform: template
    sensors:
        garage_door_status:
          value_template: >-
            {% if states.binary_sensor.garage_roller_100.state == 'on' %}
              Open
            {% elif states.binary_sensor.garage_roller_100.state == 'off' %}
              Close
            {% endif %}
          icon_template: >-
            {% if states.binary_sensor.garage_roller_100.state == 'on' %}
              mdi:garage-open
            {% elif states.binary_sensor.garage_roller_100.state == 'off' %}
              mdi:garage
            {% endif %}

which is great, but I want to associate it with a button:

type: 'custom:button-card'
tap_action:
  action: toggle
  confirmation:
    text: Toggle Garage Door?
entity: switch.garage_door_90
icon: 'mdi:garage'
show_state: false
show_name: true
show_icon: true
size: 60px
color: var(--paper-item-icon-color)
hold_action:
  action: none

How do I combine the two…

The reason I’m doing that is because my button activates a pushbutton via dry contact, so there is no feedback on state. So i have a reed switch which will pick up if it’s open or closed so I want to relay that information visually on this button rather than having a button and a sensor state glance adjacent.

Thanks.

Instead of using the button, use the sensor, and call the button as its action:

          - entity: garage_door_status
            name: Garage Door
            tap_action:
              action: call-service
              service: switch.toggle
              service_data:
                entity_id: switch.garage_door_90
              confirmation:
                text: Toggle Garage Door?

or something similar to that

Thanks Richie!

this works:

type: 'custom:button-card'
tap_action:
  action: call-service
  service: switch.toggle
  service_data:
    entity_id: switch.garage_door_90
  confirmation:
    text: Toggle Garage Door?
entity: sensor.garage_door_status
show_state: false
show_name: false
show_icon: true
size: 60px
color: var(--paper-item-icon-color)
hold_action:
  action: none