Change home/not_home status display but... Dynamically? Is it possible?

Hi everyone.
I have a need to convert the way the home/not_home/other zones status of a person entity is displayed.

Let me explain.
I have a card that displays the home status of me and my partner, but not only that, it also shows us the name of the zone we are in if it has been coded in the zones. So when it’s at work one thing appears, when I’m at the gym another and so on.

What happens?
I have already read the following posts:

And they were helpful in creating a new sensor that would allow me to rename the not_home to Not home (definitely nicer). The problem is that the way I wrote the code this ignores all the other zones. So if my partner is at work instead of seeing “Work” written I see “Not Home” written, and it doesn’t make much sense.

Here’s the code I used.

  - platform: template
    sensors:
      position_ale:
        value_template: '{% if is_state("device_tracker.redmi_note_7", "not_home") %}Not home{% else %} ?????? {% endif %}'
        friendly_name: 'Ale position' 

So do you think it is possible to apply a template that says:
If the status of person.a is not_home show Not home, otherwise show the status of person.a whatever it is. Where I put the question marks I think the sensor status should be entered person.xxx but I can’t get it to work.

Thanks

It sounds like you want a sensor to do what you say it already does.

What is it doing wrong?

I think you just need to set it to state in the else.

  - platform: template
    sensors:
      position_ale:
        value_template: '{% if is_state("device_tracker.redmi_note_7", "not_home") %}Not home{% else %} states("device_tracker.redmi_note_7") {% endif %}'
        friendly_name: 'Ale position'
1 Like

I also need to rename not_home (which sucks) to Not home (much nicer).
Otherwise yes, I agree with you that entity person already does this. So it’s a matter of having the same functionality but changing the state name not_home to Not home

Ok I get it now. I think your “else” solution is correct.

That had been my first thought but I thought I had written the wrong code.

However it doesn’t work, this is what the GUI returns:

immagine

Try this then…

  - platform: template
    sensors:
      position_ale:
        value_template: >- 
                        {% if is_state("device_tracker.redmi_note_7", "not_home") %}
                            Not home
                        {% else %} 
                            states("device_tracker.redmi_note_7") 
                        {% endif %}
        friendly_name: 'Ale position'

For a moment I was hopeful but then… :frowning:

So apparently i can’t pass the status of another entity…

ahh… never mind I see the error now.

{{ }} is needed.

  - platform: template
    sensors:
      position_ale:
        value_template: >- 
                        {% if is_state("device_tracker.redmi_note_7", "not_home") %}
                            Not home
                        {% else %} 
                            {{ states("device_tracker.redmi_note_7")  }}
                        {% endif %}
        friendly_name: 'Ale position'
2 Likes

I have no idea what that screenshot is even showing.

Yes! That seems to work!
My partner is currently at work and I actually see the correct location in her file.
As soon as she leaves to go home I’ll take a look at how the status changes.
Just for information…

What is the difference between creating a sensor with

value_template: >- (on multiple lines)

rather than with

value_template: '{% (on one line)

P.S. The pictures were just to show more quickly how the sensor status was shown, that’s all.

Great!

Mostly readability.
But I had a feeling the ' ' was causing issues since it was read as a string. But I was wrong and I’m quite sure it will work as a one liner also when you use {{ }}.

I’ll steal another moment of your time then.
Do you think it’s possible to add an elseif?

If not_home shows Not Home
elseif home shows Home
elseif shows the status sensor

Something like… This?

  - platform: template
    sensors:
      position_ale:
        value_template: >- 
                        {% if is_state("device_tracker.redmi_note_7", "not_home") %}
                            Not Home
                        {% elif is_state("device_tracker.redmi_note_7", "home") % }
                           Home
                        {% else %}
                            {{ states("device_tracker.redmi_note_7")  }}
                        {% endif %}
        friendly_name: 'Ale position'

Thanks a lot!

That looks correct

Another method is probably something like this:

{{ states("device_tracker.redmi_note_7")[0:1] | upper }}{{ states("device_tracker.redmi_note_7")[1:] | replace("_", " ") }}

This will make the first letter uppercase and rest lower (unchanged).
Then replace underscore with space.

So… I think this will solve all issues in one go.
Meaning you can make it:

  - platform: template
    sensors:
      position_ale:
        value_template: '{{ states("device_tracker.redmi_note_7")[0:1] | upper }}{{ states("device_tracker.redmi_note_7")[1:] | replace("_", " ") }}'
        friendly_name: 'Ale position'

But try it first as a separate sensor because it could create issues.

2 Likes

And it works!
nice!

I also need to translate the sensor status, so basically the first solution is already fine.
However good to know, I think I might use this method for something else

Thanks!