Grafana Entiy vs Attribute how to make use of?

Hi there!

Next step in making a home smart :wink:

I have infuxdb & grafana up and runing and now i want to see e. g. bytes_received down from the fritzbox_netmonitor in a grafana graph.

is_linked: true
is_connected: true
external_ip: x.x.x.x
uptime: 173062
bytes_sent: 966187565
bytes_received: 9012508879
transmission_rate_up: 1206
transmission_rate_down: 18273
max_byte_rate_up: 1318625
max_byte_rate_down: 6589875
friendly_name: Fritzbox Internet
icon: 'mdi:web'

I’am not able to figure out how i can access the attributes of that sensor in grafana.

I suppose (fear), that i have to ā€œconvertā€ the attributes i wish to use to a new / extra sensor. Is that true? If so how can i do that for example for the attribute ā€œbytes_receivedā€? And what do i have to arrange to have this information stored in the recorder?

Or do i miss here something and the attributes are there, but i simply do not find out?

You will need to create template sensors for the attributes.

1 Like

This means i understood that correct, thanks!

Do i have to arrange something so that this information is regulry written via recorder into the database? Or does that simply work as soon as i created a sensor / template?

Or are there ready made templates for the fritzbox_networkmonitor?

Whenever the entities in the templates update the template sensor updates and is recorded in the database. All automatic. You don’t need to do anything.

If you supply the entity_id of the sensor I’ll show you an example you can use.

Great!

I have the following sensor:

sensor.fritzbox_internet

The attributes of that sensor are

is_linked: true
is_connected: true
external_ip:x.x.x.x
uptime: 273062
bytes_sent: 966187565
bytes_received: 9012508879
transmission_rate_up: 1206
transmission_rate_down: 18273
max_byte_rate_up: 1318625
max_byte_rate_down: 6589875
friendly_name: Fritzbox Internet
icon: 'mdi:web'

I like to go for

is_linked
is_connected
bytes_sent
bytes_received

The first two are binary sensors (true/false). So use binary template sensors:

binary_sensor:
  - platform: template
    sensors:
      is_linked:
        friendly_name: "Is Linked"
        value_template: "{{ state_attr('fritzbox_internet', 'is_linked') }}"
        device_class: connectivity
      is_connected:
        friendly_name: "Is Connected"
        value_template: "{{ state_attr('fritzbox_internet', 'is_connected') }}"
        device_class: connectivity

The device class picks which icon and words are displayed. See here for more: Binary Sensor - Home Assistant

The second two are sensors, if you give them a unit you can graph them. I chose to convert bytes to megabytes but you could use anything you want, bytes, kilobytes, gigabytes, etc… just by changing the the template divisor.

sensor:
  - platform: template
    sensors:
      bytes_sent:
        friendly_name: "Bytes Sent"
        unit_of_measurement: "MB"
        value_template: "{{ ( state_attr('fritzbox_internet', 'bytes_sent')|int / 1024 )round(2) }}"
      bytes_received:
        friendly_name: "Bytes Received"
        unit_of_measurement: "MB"
        value_template: "{{ ( state_attr('fritzbox_internet', 'bytes_received')|int / 1024 )round(2) }}"

I also rounded the result to two decimal places. Again, feel free to change this.

The |int filter is used to change strings to integers (so we can do division). It may not be required. Attributes can have other types that just string where as states are always strings.

1 Like

Impressive!

Thank you very, very much! I will try that asap and give feedback!

Hi @tom_l

thanks again for you code.

I could not compile the second part. Got syntax error.

I tried this to correct it:

- platform: template
  sensors:
    bytes_sent:
      friendly_name: "Bytes Sent"
      unit_of_measurement: "MB"
      value_template: "{{ (state_attr('fritzbox_internet', 'bytes_sent')|int / 1024|round(2)) }}"
    bytes_received:
      friendly_name: "Bytes Received"
      unit_of_measurement: "MB"
      value_template: "{{ (state_attr('fritzbox_internet', 'bytes_received')|int / 1024)|round(2) }}"

This allowed me to restart. Can you double check if my guessing for the ā€œcorrectionā€ is correct?

Just if someone else comes to this posts later on.

I will wait now for the collection of the data and have a look later this evening or tomorrow.

Just for my understanding: after the div i got floats automatically, right? Otherwise the ā€œroundā€ would not make sense, or?

What error did you get?

At the moment you are only rounding the number 1024.

value_template: "{{ (state_attr('fritzbox_internet', 'bytes_sent')|int / 1024|round(2)) }}"

You missed a )

I get this one

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['bytes_received']['value_template']. Got "{{ (state_attr('sensor.fritzbox_internet', 'bytes_received')|int / 1024)|round(2)) }}". (See ?, line ?).`

I changed it one more time and compared the original sensor and it’s attributes and the new four sensors based on them.

I then found another problem: wie missed to add ā€œsensor.ā€ to the sensor name where the new ones are based on: sensor.fritzbox_internet

So now i have the following for the binary sensors

- platform: template
  sensors:
    is_linked:
      friendly_name: "Is Linked"
      value_template: "{{ state_attr('sensor.fritzbox_internet', 'is_linked') }}"
      device_class: connectivity
    is_connected:
      friendly_name: "Is Connected"
      value_template: "{{ state_attr('sensor.fritzbox_internet', 'is_connected') }}"
      device_class: connectivity

and this for the byte/Mbyte sensors

- platform: template
  sensors:
    bytes_sent:
      friendly_name: "Bytes Sent"
      unit_of_measurement: "MB"
      value_template: "{{ ((state_attr('sensor.fritzbox_internet', 'bytes_sent')|int / 1024)|round(2)) }}"
    bytes_received:
      friendly_name: "Bytes Received"
      unit_of_measurement: "MB"
      value_template: "{{ ((state_attr('sensor.fritzbox_internet', 'bytes_received')|int / 1024)|round(2)) }}"

And everything works! Including the rounding :sweat_smile: Doublechecked. The values of the new sensors reflect the attributes of the main sensor in the development panel.

Perfect. Thanks again!

Sorry about the missing sensor domains. Must be time for bed. You have extra brackets that are not required. Try this

- platform: template
  sensors:
    bytes_sent:
      friendly_name: "Bytes Sent"
      unit_of_measurement: "MB"
      value_template: "{{ (state_attr('sensor.fritzbox_internet', 'bytes_sent')|int / 1024)|round(2) }}"
    bytes_received:
      friendly_name: "Bytes Received"
      unit_of_measurement: "MB"
      value_template: "{{ (state_attr('sensor.fritzbox_internet', 'bytes_received')|int / 1024)|round(2) }}"
1 Like

Don’t worry. I’am not after ā€œcut & paste& bye byeā€ (mostly) solution. I also want to understand what is going on to help myself next time :slight_smile:

I check you additional hints! Thank you!

1 Like

Do not need to test. It is absolute clear that the outer brackets are useless :slight_smile:

1 Like

Good! That is why I included explanations.

1 Like

Hate to dig up a really old thread… but I’m having a similar issue trying to map an attribute and I’ve gone through templates but I’m just not getting it.

I’ve managed to map the attribute to an entity card configuration and have the attribute value showing that way, but I can’t seem to get the attribute to list in grafana.

The entity card code is:

type: entity
entity: sensor.storage_c
attribute: UsedSpacePercentage
unit: ā€˜%’
state_color: true

This shows % used space on the card, no issue.

Then, I try to get the value into grafana, and I can’t make it populate. This is the select statement code when I switch from what I’ve mapped in visual editor:

SELECT mean(ā€œvalueā€) FROM ā€œautogenā€.ā€œ%ā€ WHERE (ā€œentity_idā€::tag = ā€˜storage_c’) AND $timeFilter GROUP BY time($__interval) fill(null)

Obviously, I don’t have the attribute defined anywhere there but I can’t figure out the syntax to define it.

Any help, very much so appreciated. If you require more information please let me know.

Attributes are not imported into InfluxDB/Grafana.