Change the text of some attributes?

I want to alter some attributes shown in Home Assistant, and need a little help.

For ‘device_tracker.jonos_iphone’ it shows me as ‘home’ when I’m home, but I want to capitalise it (Home). I guess that’s the OCD in me :smile:
I’m guessing I can do it with a value template, but I’m not sure how.

Also, I use this to show my battery level ‘sensor.jonos_iphone_battery_level’.
It shows the battery level as a number, but I want to add the percent sign (%) to the end. Again, I’m guessing I can do it with a value template, but I’m not sure how.

To change the name of the location, you just have to create a zone to over-write the default home zone.

For the battery percent, you can create a sensor that extracts its value from the tracker attributes like this:

mybattery:
  value_template: '{{ states.device_tracker.jonos_iphone.attributes.battery }}'
  unit_of_measurement: '%'
  friendly_name: 'My Battery'

Thanks a lot, I forgot about overriding the default home zone :slight_smile:

I’ve tried it but for some reason it didn’t alter the name. Do you know if I should remove the latitude and longitude from above (under homassistant:), of if I should leave that there?

I tried battery percentage thing but that doesn’t work at the moment either (doesn’t show up as a sensor under Current Entities in the Dev Tools).
I looked at the Template Sensor page on Home Assistant’s website and the code seems right. I guess I’ll need to play around a little to get that working.

The only way to over-ride the home zone is to call it home.
I have lat and lon both in the main config file and also in the zone file.
In my zones section I have
> - name : HOMe
> latitude: !secret latitude
> longitude: !secret longitude
> radius: 250
> icon: mdi:account-multiple

then in the developers state page I see:

> zone.home	    hidden: true
>                      latitude: REDACTED
>                      longitude: REDACTED
>                      radius: 250
>                      friendly_name: HOMe
>                      icon: mdi:account-multiple
>                      track_ios: true

You can see that the friendly name in the zone matches exactly with the case that I typed it.

The battery code that I posted came directly from my working configuration, so it’s just a matter of getting the YAML correct, and it should be fine.

@jono Is not asking how to change the name of the zone, but the name of the state.

Ex: When he arrives home, he would like his status to show “Home” instead of “home”

I have also been struggling to figure this out.

OK, you could make a new template sensor with the template:

value_template: '{%if states.device_tracker.jonos_iphone.state == "home"%}Home{%else%}Not Home{%endif%}'

1 Like

Thanks, I got the code working for adding the percent sign :+1:t3: (needed to add step of indentation)

If I use this code, when someone is in another zone I’ve setup in HA (e.g. my wife’s in the ‘Work’ zone or my son’s in the ‘School’ zone would HA still show this, or just ‘Not Home’ when they’re not at home?

Yes, it will still show the zone name you have configured under zones.
This will only change “home” to “Home” unless you specify otherwise.

Great, thanks!

…so I added both the ‘regular’ one (device_tracker.wifes_iphone) and the value_template one from here to my HA config.

My wife went to work and the regular one says she’s at ‘Work’ (work zone) and the value_template one says she’s ‘Away’. So I guess it doesn’t change to reflect the zone with this value_template version :-1:

I don’t suppose there’s a way of changing ‘home’ to ‘Home’ with a value_template, without specifying an ‘{%else%}Not Home’ part is there?

You are correct, looking at the argument specified, we are telling HA she’s either Home or Not Home, I’m not sure of if you can specify without the else effect.

Try the following: add an elif for each zone

Sensor:
  - platfor: template
    sensor
	  device_tracker.jonos_iphone:
	    friendly_name: 'Jonos'
		value_template: >-
		    {%- if is_state("device_tracker.jonos_iphone", "home") %}
			    Home
			{%- if is_state("device_tracker.jonos_iphone", "work") %}
			    Work
			{%- if is_state("device_tracker.jonos_iphone", "school") %}
			    School
			{% else %}
			    Not Home
			{% endif %}

Thanks, I’ll give this a try.

If you are just trying to change the first letter to upercase, could you not use the title() function that jinja provides?
I am not very good at templating and can’t test it right now, but something like this:

title( is_state("device_tracker.jonos_iphone") )

from the documentation here: http://jinja.pocoo.org/docs/dev/templates/

title(s)
Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.

I’m sure it applies, but just like you, I’m probably worse with templates.

I have no idea where it will apply to change the state.

I may be wrong, but to me, your example will change the entity and not the state.

So far the multi-line template sensor from Coolie1101 is working well. I’ll stick with that for now :slight_smile:
(as I don’t know anything about jinja)

OK, so I tried that.

This capitalises every word.

before: {{ states("device_tracker.020fb50569b7") }}
after:  {{ states("device_tracker.020fb50569b7")|replace("_"," ")|title }}

Since not_home is one word I also replace “_” with " " and the result is:

before: not_home
after:  Not Home

this should always work and you don’t need to change your template when there is a new location.

1 Like

Thanks, I’ve changed my config to this, and will see how it goes :slight_smile:

[EDIT] So far so good, it’s working well :thumbsup: