First sensor attempt fail

Due to major problems with HA and location services on my Pixel 6 after Android 13 I have decided to use my phones wifi connection to determine if I am home or not and am almost there but not quite.

I added a binary sensor to my configuration.yaml to show either “home” or “not home” so I can create automations based on that but something is not right. The state of the sensor always shows “off” even though the state of the pixel 6 wifi connection is correct.

I have no idea what I am doing wrong and holping someone can asssit me.

Here is what is in my config:

binary_sensor:
  - platform: template
    sensors:
      roberts_pixel_6_home:
        friendly_name: 'Roberts Pixel 6 Home (SSID = Maximilian)'
        device_class: "presence"
        value_template: >-
          {% if states('sensor.pixel_6_wifi_connection') == "Maximilian" %}
          home
          {% else %}
          not_home
          {% endif %}

I was expecting the value for roberts_pixel_6_home to either show home or not home but it justs shows off

Thanks

OK, well first of all - this is using the old style of template sensors - and since this a new sensor, you should really be using the new style:

Second - if it’s a binary sensor, then the state can only be on or off, not home and not_home.

template:
  - trigger:
      - platform: state
        entity_id: sensor.pixel_6_wifi_connection
    binary_sensor:
      - name: "Roberts Pixel 6 Home (SSID = Maximilian)"
        state: "{{ trigger.to_state.state == 'Maximilian' }}"
        device_class: presence
        unique_id: wifipresence-df3229611cf24749ace9487f2ddc2f19

For the state, because it is a binary sensor and can only be true or false, then you only need to provide a single test - and because the sensor is updated with the state of the wifi connection sensor changing - then we can get direct access to the state of the trigger. Thus if trigger.to_state.state does not match the name of the WiFi network (Maximilian) - then the evaluation is false - and the sensor is off.

I’m not sure what entity id it would end up giving this binary sensor by the way, you would be better off with something like:
name: roberts pixel 6 presence or something. Because we have provided a unique_id: then you can change the actual name of the binary sensor in the UI

Thanks a lot for the quick reply and detailed information. Binary 0/1 duh LOL.

I will go over the template sensor instructions and get some learning in. I have been using old YouTube videos to get me through it apparently.

Your reply was very valuable for me to learn more, and I really appreciate it.

1 Like