Issue with template and mac address

hello, i have a strange problem with an ip address template on a device.tracker.
I always have a “TemplateSyntaxError: expected token 'end of print statement” error when I try to retrieve the @ ip from the device trackers which starts with a number in the mac address.
But it works great for those who start with a letter!
Does anyone have an explanation to this ?

  - platform: template
    sensors:
      harald_pc_ip:
        value_template: '{{ states.device_tracker.f4_xx_xx_xx_xx_ff.attributes.ip }}'
        unit_of_measurement: ''
      livebox_ip:
        value_template: '{{ states.device_tracker.a4_xx_xx_xx_xx_25.attributes.ip }}'
        unit_of_measurement: ''    
      david_pc_ip:
        value_template: '{{ states.device_tracker.30_xx_xx_xx_xx_b0.attributes.ip }}'
        unit_of_measurement: ''
      mathilde_pc_ip:
        value_template: '{{ states.device_tracker.00_xx_xx_xx_xx_ba.attributes.ip }}'
        unit_of_measurement: ''
      

Do you have an example of what you’re talking about? Because the template sensor definitions above should be fine (although generally I’d always recommend using the state_attr() function.)

1 Like

You have to use square brackets for entity object ids that start with a number. e.g.

value_template: '{{ states.device_tracker[00_xx_xx_xx_xx_ba]attributes.ip }}'

See: https://www.home-assistant.io/docs/configuration/templating/#entity_id-that-begins-with-a-number

And taking Phil’s advice into account:

value_template: "{{ state_attr('device_tracker[00_xx_xx_xx_xx_ba]', 'ip' }}"
1 Like

D’oh! Ok, now I see. Looked right past the tracker names. I thought the OP was saying the problem was with the values of the ip attribute.

@tom_l, you’re close, but not quite…

Either:

value_template: '{{ states.device_tracker["30_xx_xx_xx_xx_b0"].attributes.ip }}'

or, better yet:

value_template: '{{ state_attr("device_tracker.30_xx_xx_xx_xx_b0", "ip") }}'
2 Likes

True
The last syntax with

value_template: '{{ state_attr("device_tracker.30_xx_xx_xx_xx_b0", "ip") }}'

works fine and returns the ip address as desired
Thank you !

1 Like