Device_tracker "Online" instead of "Home" - How to template a sensor?

Hi there,

I’m new to HA, and I’m struggling to understand how to work with the configuration.yaml. I’m trying to convert some of my device_tracker.device devices (via Unifi Integration) from saying “Home” to “Online”, or another arbitrary string.

I’ve found a few topics on this forum about binary sensors, templates and so on, but I can’t get any of it to work. The below is taken from the HA site, it looks like what I want to do but it doesn’t change anything.

template:
    - sensor:
        - name: "frontcam"
          state: >
            {% if is_state('device_tracker.device', 'home') %}
                Online
            {%else%}
                Offline
            {%endif%}

Could anyone help me?

Got there. It’s probably my lack of understanding on yaml formatting, it looks sort of similar to the example.

I did:

sensor:
    - platform: template
      sensors:
        cam_online:
            friendly_name: "Camera"
            entity_id: device_tracker.cam
            value_template: "{% if is_state('device_tracker.cam', 'home') %}Online{% else %}Offline{% endif %}"

Do you mean sensor.frontcam fails to be created or it is created but its state never changes?

If it fails to be created, you have to execute Configuration > Server Controls > Reload Template Entities in order to load it.


EDIT

Functionally, there’s no difference between the two versions you posted except that the first one is in the new ‘modern’ format and the second one is in ‘legacy’ format. Going forward, the emphasis will be on using the ‘modern’ format.

Thanks for your help!