Struggling with code

Hi all,

Slowly working my way through Home Assistant. It has been a fair while since I’ve done any form of codes, and its hard to fit back into the grove.

After battling to get themes and cards working correctly (3 day effort!) I’m now up to trying to get a conversion done to add to one of my cards.

My Ubiquiti Dream Machine provides me both sent and received values in KB/s and I am wanting to turn this into Mbps so I can display it easier on a card. I’ve struggled with this for the last few hours, and still haven’t found a solution. I think I’ve become confused about sensors, templates and how to use them all.

The following two strings are provided by my device sensor.unifi_dream_machine_kib_s_received and sensor.unifi_dream_machine_kib_s_sent

I’ve been playing around with the template editor in developer tools, and have managed to get what I want to display in the result type

  - sensor:unifi_dream_machine_kib_s_sent
      - name: "WAN Upload speed"
        unit_of_measurement: "Mbps"
        state: {{ states('sensor.unifi_dream_machine_kib_s_sent') | int / 1024 }}

Result type: string
template:
  - sensor:unifi_dream_machine_kib_s_sent
      - name: "WAN Upload speed"
        unit_of_measurement: "Mbps"
        state: 0.005859375
This template listens for the following state changed events:

Entity: sensor.unifi_dream_machine_kib_s_sent

however, I am struggling to work out how to turn this into an entity(?) to make it display on a card.

I feel as though I am incredibly close, however I’ve blown 6 hours on this tonight and haven’t had any luck.

Appreciate any pointers (I’m sure once I can get this one out of the way and see how it works, I’ll be able to do anything I need with conversion!)

Thanks

Hi, @Kazzaw - welcome to the forums!

If you’ve gone through the template editor, then you’ve done the hard work!

Here’s what you need to do next to make this work:

  • Add the following code under your sensor key in your configuration file. It will look like this:
sensor:

If it’s not there, then add it.

Then add this code under the sensor: key (I’ve included it for completeness). Make sure you have the indenting correct as shown below - otherwise, you’ll get an error.

sensor:  #               <--- this is the keyword that all sensors are included under in your config file
  - platform: template # <--- this lets us use templates in the sensor
    sensors: #           <--- this tells HA that we have a list of sensors following (there's only one in this case)
      wan_upload_speed: #<--- this is the name of the sensor you are creating
        friendly_name: "WAN Upload Speed"    
        unit_of_measurement: "Mbps"
        value_template: "{{ states('sensor.unifi_dream_machine_kib_s_sent') | int / 1024 }}"

I tested this to an extent, although I don’t have a dream_machine, so used different sensors.

2 Likes

This contains errors:

template:
  - sensor:unifi_dream_machine_kib_s_sent
      - name: "WAN Upload speed"
        unit_of_measurement: "Mbps"
        state: {{ states('sensor.unifi_dream_machine_kib_s_sent') | int / 1024 }}
  1. The template should be bounded by double-quotes but it isn’t.
  2. The documentation doesn’t indicate the following convention is supported:
    - sensor:unifi_dream_machine_kib_s_sent

This adheres to the documentation’s standards:

template:
  - sensor:
      - name: "WAN Upload speed"
        unit_of_measurement: "Mbps"
        state: "{{ states('sensor.unifi_dream_machine_kib_s_sent') | int / 1024 }}"

Thanks! This has me sorted!

1 Like

For the record, correcting the mistake didn’t require changing the sensor’s configuration from ‘modern’ format to ‘legacy’ format. All that was required was to fix two syntax errors.

Excellent point, @123 - I have a TON of “legacy” templates and haven’t quite moved into the “modern” format yet… but am at least now thinking about how I want to structure those. Might be a good opportunity to revisit all my aging templates to look for better (more efficient) ways.

Nonetheless, this thread now has a so-called Solution that misleads others to believe incorrect ‘modern’ format is fixed by changing it to ‘legacy’ format. :-1: