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

Good evening,
sorry for the absurd question, but how can I make the icon of an entity button light up when I press and the status of a sensor is on and turn off when I press and the status of a sensor is off?

Thanks, Alberto

alias: your automation name here
trigger:
  platform: state
  entity_id: switch.your_button_entity_id
action:
  service_template: >
    {% if is_state('sensor.your_sensor_entity', 'on' %}
      light.turn_on
    {% else %}
      light.turn_off
    {% endif %}
  entity_id: light.your_light_entity

that’s the best i can do with the limited info you gave.

use custom-ui and this:

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

works perfectly both in Legacy and Lovelace

1 Like

Ok finity.

I try to give more details.
I have outputs that I control via modbus.
The outputs control step by step relays
I have inputs connected to the relay that tell me the status of the relay.

So according to the configuration in HA I have:
1 Sensor that indicates the status (on/off) of the light controlled by the step/step relay
2 A switch that switches the state of the relay.

In this mode work all but:
in this situation I have the switch icon that lights up when I press it and if the light is off it turns on but the button icon goes off.
If we repeat the light turns off and the switch icon turns on and then turns off.
While the sensor status shows the icon correctly because it works based on the input status.

Now my aim would be to have the switch icon connected to the sensor status

I hope I haven’t messed up even more
Alberto

Sorry I missed the part about the icon. It was late when I was reading that. :wink:

My recommendation only switches the state of the light.

Never mind my post above.

I think I need to clarify more tho.

do you have two entities (only a switch and a light) or three entities (a switch, a light and a button)?

First you said this:

but then you said this:

is the “switch” you are talking about in both of those statements a “button”? or are those two different devices? I think they are the same device and that you are just mixing up the terminology in that second part but I just want to clarify.

Which “icon” are you referring to? the one to the right side of the switch/button (the toggle) or the one to the left?

I’m not trying to be difficult I just want to be sure I’m understanding exactly what you have and what you want.

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.