Dbm to bars

How can I convert dbm to signal strangth bars to display in home assistant.

The logs show the bars (see image) but it gets reported as a -ve dbm value and I would like to show this as a percentage or number of bars in HA ?

And what is the formulae to convert from dbm to bars

I wouldn’t bother with a formula.

If you want to match dBm to say a 4 bar chart - use these values for example:

sensor:
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    id: wifi_signal
    update_interval: 60s

  - platform: template
    name: "WiFi Number of Bars"
    lambda: |-
      if (id(wifi_signal).state >= -30)  {
        return 4;
      } else if (id(wifi_signal).state < -30 && id(wifi_signal).state >= -67) {
        return 3;
      } else if (id(wifi_signal).state < -67 && id(wifi_signal).state >= -70) {
        return 2;
      } else if (id(wifi_signal).state < -70 && id(wifi_signal).state >= -80) {
        return 1;
      } else {
        return 0;
      } 
    update_interval: 60s

Apologies for any syntax errors - untested but you get the drift. The example values I used are from Wi-Fi Signal Strength Basics | MetaGeek but feel free to Google for your own values.

Thanks so much for helping, for some reason I was unable to return a string / integer, this is what works for me:

sensor:
  - platform: wifi_signal
    id: wifi_sig
    name: Signal
    update_interval: 60s

text_sensor:
  - platform: template
    name: "Wifi Bars"
    entity_category: diagnostic
    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";
	    }
    update_interval: 60s


Ah - that’s probably my bad… should have used a Template Number sensor. Not sure why my brain said Template Text.

If all you doing is a comparison for how many bars to display it shouldn’t matter.