Unable to Set icon

Can’t figure out what’s wrong with this, the state value shows values > 0 however the displayed icon is permenantly showing the “wifi-strength-alert-outline” icon.

What am I doing wrong ?

sensor:
  - platform: wifi_signal
    id: wifi_sig
    name: Signal

  - platform: template
    name: "Signal"
    id: wifi_bars
    entity_category: diagnostic
    on_raw_value:
      then:
        - lambda: |-
            if(x==4.0) id(wifi_bars).set_icon("mdi:wifi-strength-4");
            else if(x==3.0) id(wifi_bars).set_icon("mdi:wifi-strength-3");
            else if(x==2.0) id(wifi_bars).set_icon("mdi:wifi-strength-2");
            else if(x==1.0) id(wifi_bars).set_icon("mdi:wifi-strength-1");
            else id(wifi_bars).set_icon("mdi:wifi-strength-alert-outline");

Im not sure exactly what value X is at the time your lambda test happens but based on your structure you’re falling into the else clause because nothing matched.

Is the data type on X a number or a string value?

Thanks, I have re-written this to use a string value, the template set’s it’s state value based on the signal strength from wifi_sig.

sensor:

  - platform: wifi_signal
    id: wifi_sig
    name: Signal
    update_interval: $refresh

text_sensor:
 - platform: template
    name: "X Signal"
    id: wifi_bars
    entity_category: diagnostic
    update_interval: $refresh
    on_raw_value:
      then:
        - lambda: |-
            ESP_LOGI("main", "Value of my sensor: %s", x);
            ESP_LOGI("main", "State: %s", id(wifi_bars).state);
            if(x=="4") id(wifi_bars).set_icon("mdi:wifi-strength-4");
            else if(x=="3") id(wifi_bars).set_icon("mdi:wifi-strength-3");
            else if(x=="2") id(wifi_bars).set_icon("mdi:wifi-strength-2");
            else if(x=="1") id(wifi_bars).set_icon("mdi:wifi-strength-1");
            else id(wifi_bars).set_icon("mdi:wifi-strength-alert-outline");

    lambda: |-
        if (id(wifi_sig).state >= -30) return (std::string) "4";
        else if (id(wifi_sig).state < -30 && id(wifi_sig).state >= -67) return (std::string) "3";
        else if (id(wifi_sig).state < -67 && id(wifi_sig).state >= -70) return (std::string) "2";
        else if (id(wifi_sig).state < -70 && id(wifi_sig).state >= -80) return (std::string) "1";
        else return (std::string) "0";

Strange thing is although the state of the sensor reflects the correct state of “0” to “4”, when I log the variable x or if I get the sensor state instead, it is not the state value:

[14:31:50][D][sensor:127]: 'Signal': Sending state -62.00000 dBm with 0 decimals of accuracy
[14:31:54][D][text_sensor:067]: 'X Signal': Sending state '3'
[14:31:54][I][main:046]: Value of my sensor: 0\xca?
[14:31:54][I][main:047]: State: 0\xca?

I think that value is hex and is the actually signal strength in dB (assuming a signed int): -54. That’s why it executes the if that gives 3.

Even this does not set the icon, yet the number of bars values (“0” > “4”) are set correctly !

  - platform: template
    name: Wifi Signal"
    id: wifi_bars
    icon: "mdi:wifi-strength-alert-outline"
    entity_category: diagnostic
    update_interval: $refresh

    lambda: |-
        
        if (id(wifi_sig).state >= -30) {
          id(wifi_bars).set_icon("mdi:wifi-strength-4");
          return {"4"};
        } else if (id(wifi_sig).state < -30 && id(wifi_sig).state >= -67) {
          id(wifi_bars).set_icon("mdi:wifi-strength-3");
          return {"3"};
        } else if (id(wifi_sig).state < -67 && id(wifi_sig).state >= -70) {
          id(wifi_bars).set_icon("mdi:wifi-strength-2");
          return {"2"};
        } else if (id(wifi_sig).state < -70 && id(wifi_sig).state >= -80) {
          id(wifi_bars).set_icon("mdi:wifi-strength-1");
          return {"1"};
        } else {
          // id(wifi_bars).set_icon("mdi:wifi-strength-alert-outline");
          return {"0"};
        }

Screenshot 2022-10-08 at 3.43.11 PM

Are you certain you can change the icon at “runtime”? Does HA even support that? :thinking:

You might be onto something, so I Googled: Icon template with ESPHome cover.

Well I’ve always assumed that this whatever state / attributes changed would be reflected in the UI ?!

Hi,
I am trying similar thing but I don’t have lovelace installed.
I am trying to change state of my wifi icon , based on the signal strength of the wifi_signal.

I have added following to the yaml file


- platform: wifi_signal

    name: "Wifi_signal_db"

    id: wifi_signal_db

    update_interval: 5s

    entity_category: "diagnostic"

  - platform: copy

    source_id: wifi_signal_db

    id: room1_db

    name: "Wifi Signal %"

    filters:

    - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);

    icon: >-

          {% if sensor.room1_wifi_signal >= '52' %} mdi:wifi-strength-4

          {% elif sensor.room1_wifi_signal < '52' %} mdi:wifi-strength-3

          {% endif %}

    unit_of_measurement: "%"

    entity_category: "diagnostic"

I am gettting following errors

Icons must match the format "[icon pack]:[icon]", e.g. "mdi:home-assistant".

As adanced users here can see, I am new to home assistant, can somebody guide me in solvoing the problem ?

Thanks and Regards

Hi,
I a begineer to home assistant, seeing the above example , i have modified the room1.YAML ( which is my esp32 file).

esphome:
  name: "room1"
  friendly_name: room1

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "KKaUZtsqqkZuzECMn7rWXmSyNaj/NCfQrwJw6oN/2Bk="

ota:
  
i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-Cb77A0"
    password: "dqW2cJNSPWHg"

captive_portal:

sensor:
  - platform: sht4x
    precision: Med
    heater_time: Short
    update_interval: 5s
    temperature:
      id: r1tem
      name: "Temperature"
      accuracy_decimals: 3
    humidity:
      id: r1hum
      name: "Relative Humidity"
      accuracy_decimals: 3
  - platform: wifi_signal
    name: "Wifi_signal_db"
    id: wifi_signal_db
    update_interval: 5s
    entity_category: "diagnostic"
  - platform: copy
    source_id: wifi_signal_db
    id: room1_db
    name: "Wifi Signal %"
    filters: 
    - lambda: >
            int y=min(max(2 * (x + 100.0), 0.0), 100.0);
            if(y>=85) id(room1_db).set_icon("mdi:wifi-strength-4");
            else if (y>65) id(room1_db).set_icon("mdi:wifi-strength-3");
            else if (y>30) id(room1_db).set_icon("mdi:wifi-strength-2");
            else if (y>10) id(room1_db).set_icon("mdi:wifi-strength-1");
            else id(room1_db).set_icon("mdi:wifi-strength-off-outline");
            return y;
    unit_of_measurement: "%"
    entity_category: "diagnostic"

Once , I upload the file to ESP after validation , I am able to see the icon change, but it happens only once.i.e
The icon is not changed dynamically, when after the reset page open, the icon is stuck to the same icon, how ever the value of wifi-signal is changed but the change in icon is not happening.
Can anyone guide in right direction please.

Thanks and Regards
( I am using HomeAssistant installed in Rpi4 as OS)