Attribute as sensor

Hello,

I have an entity sensor.netzwerk with attributes:

[...]
transmission_rate_up: 1253
transmission_rate_down: 6672
[...]

I want to use these attributes as a sensor. Therefore I add in my configuration.yaml

sensor:
[...]
  - platform: template
    sensors: 
      transmission_rate_up:
        value_template: '{{ sensor.netzwerk.attributes.transmission_rate_up|int }}'  
        friendly_name: 'Upload Transmission Rate' 
        unit_of_measurement: 'kb/s'
      transmission_rate_down:
        value_template: '{{ sensor.netzwerk.attributes.transmission_rate_down|int }}'  
        friendly_name: 'Download Transmission Rate' 
        unit_of_measurement: 'kb/s'

at startup this gives me 'sensor' is undefined errors at startup

[homeassistant.components.template.sensor] Could not render template Download Transmission Rate: UndefinedError: 'sensor' is undefined
[homeassistant.components.template.sensor] Could not render template Upload Transmission Rate: UndefinedError: 'sensor' is undefined

resulting in a state of unavailabe.

What can I do to fix that?

Another, secondary, question:

[homeassistant.components.template.sensor] Template sensor transmission_rate_up has no entity ids configured to track nor were we able to extract the entities to track from the value template(s)>

ok, I will add entitiy_id to the configuration, but shouldn’t the system be able to infer it from the very simple value_template?

Best Thanks!

Add “states.” in front of “sensor.netzwe…”

“{{ states(‘sensor.netzwerk.attributes.transmission_rate_down’) | int }}”
would be better still, as it avoids errors on startup

1 Like

Thank you both!

I tried both variants in the template editor of the web gui:

{{ states('sensor.fritzbox.attributes.transmission_rate_down') | int }}
{{ states.sensor.fritzbox.attributes.transmission_rate_down | int }}

The first one returns always 0, whereas the latter returns the expected value.

use this for the first method above and see if it makes a difference:

{{ state_attr('sensor.fritzbox', 'transmission_rate_down') | int }}
1 Like

Thanks, that works!

Can you explain to me, why state_attr('sensor.fritzbox', 'transmission_rate_down') is better than states.sensor.fritzbox.attributes.transmission_rate_down? @Mutt mentioned, that it avoids errors on startup? What does that mean?

Thanks!

If the sensor hasn’t been initialized and/or doesn’t have any data then there will be an error written into the log when the system tries to access the informnation.

If you use the states() method then the results are None. Otherwise the results are unknown and the system couldn’t properly render the template which results in the error.

2 Likes

As finity explained, but the statements : -
is_state(), states() and state_attr() - (finity, can’t think of any others off the top of my head, what have I missed?)
Are specifically designed to cope with these sort of situations. And so should be used wherever possible.

Though I have to admit that I do use “states.” in the template editor when I’m exploring some entities, but usually wrap the results in the above formats :man_shrugging: