Problem with "Nested Custom Sensors"

Hi,
I defined a custom sensor for WiFi location tracking and I was trying to add an additional custome sensor for overall location which uses the previously created custom sensor (WiFi) and the already available tracker sensor (GPS).
The problem is that reloading the configuration now I can find only the last custom sensor created the WiFi one seems not available anymore to be used (i.e. I had in the dashboard some already created cards that now show this sensor as unavailable)

Below the code in which with the second sensor the aim is to rely also on the GPS for reinforcing “Location X”. At the end only the Device location presence sensor is available for use the other no more

- sensor: 
    - name: Device_WiFi_location_presence
      state: >
          {% elif (states('sensor.device_wifi_bssid') == "xx:xx:xx:xx:xx:xx") %}
            Location X
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device_location_presence
      state: >
          {% if (states('Device_WiFi_location_presence') == "Location X") or (states('device_tracker.device') == "Location X") %}
            Location X
          {% else %}
            Outside
          {% endif %}

This is likely related to the method you are using include/merge your files. The sensor key should be able to accept a list, so just remove the second - sensor: then reload Template entities or restart HA.

EDIT: :man_facepalming:

Hi Didgeridrew,

thanks for the quick feedback, I exploit it to detail better, looking at the configuration below, I started having only the sensor 1 and 3 (WiFi related) and it worked. Then I added the sensor 2 and 4 and starting from that time only sensor 2 and 4 are available, 1 and 3 no more.
Btw I tried also to follow your instructions (as per the second configuration) for sensor 1 and 2 with the list and in this case I cannot se neither sensor 1 nor sensor 2.

- sensor: 
    - name: Device_WiFi_location_presence
      state: >
          {% elif (states('sensor.device_wifi_bssid') == "xx:xx:xx:xx:xx:xx") %}
            Location X
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device_location_presence
      state: >
          {% if (states('Device_WiFi_location_presence') == "Location X") or (states('device_tracker.device') == "Location X") %}
            Location X
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device2_WiFi_location_presence
      state: >
          {% elif (states('sensor.device2_wifi_bssid') == "yy:yy:yy:yy:yy:yy") %}
            Location Y
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device2_location_presence
      state: >
          {% if (states('Device2_WiFi_location_presence') == "Location Y") or (states('device2_tracker.device') == "Location Y") %}
            Location Y
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device_WiFi_location_presence
      state: >
          {% elif (states('sensor.device_wifi_bssid') == "xx:xx:xx:xx:xx:xx") %}
            Location X
          {% else %}
            Outside
          {% endif %}: 
    - name: Device_location_presence
      state: >
          {% if (states('Device_WiFi_location_presence') == "Location X") or (states('device_tracker.device') == "Location X") %}
            Location X
          {% else %}
            Outside
          {% endif %}

I would imagine the problem is the elif without a preceding if — there’s probably an error in your logs about this.

2 Likes

That’ll be why…

sensor.device_wifi_location_presence

You missed the sensor. and additionally ALL sensors are saved in lowercase internally, so you must use lowercase to access them.

1 Like

Riddled with errors then. You should try out these templates in Developer Tools / Templates before putting them in sensor configs.

1 Like

Interestingly, that (the second bit) is not the case although it is good practice.

I guess it’s just when you access them via states.sensor.my_sensor.last_changed

Did you check if state_attr, is_state behave the same way? It would be a very Home Assistant thing for one function to behave differently to the others.

Hi all,
thanks for your feedback, sorry for the elif it was a copy & past mistake for anonymizing the data.

Btw I corrected it and also putting sensor in low-case as below but the situation is the same :frowning:

- sensor: 
    - name: device_wifi_location_presence
      state: >
          {% if (states('sensor.device_wifi_bssid') == "xx:xx:xx:xx:xx:xx") %}
            Location X
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device_location_presence
      state: >
          {% if (states('device_wifi_location_presence') == "Location X") or (states('device_tracker.device') == "Location X") %}
            Location X
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: device2_wifi_location_presence
      state: >
          {% if (states('sensor.device2_wifi_bssid') == "xx:xx:xx:xx:xx:xx") %}
            Location Y
          {% else %}
            Outside
          {% endif %}
- sensor: 
    - name: Device2_location_presence
      state: >
          {% if (states('device2_wifi_location_presence') == "Location Y") or (states('device2_tracker.device') == "Location Y") %}
            Location Y
          {% else %}
            Outside
          {% endif %}

KR

Do you mean I should write

sensor.device_wifi_location_presence?

Thanks

Yes, because states('device_wifi_location_presence') is nothing, it doesn’t exist.

1 Like

Sorry, but this works, it’s the old sensor the WiFi related that is not apeearing anymore. The new one that is using in the logic the previous sensor (WiFi) combining WiFi with GPS data works.

You would likely be better off just setting up an automation to push the location name to a device tracker entity using the device_tracker.see service than setting up all these sensors…

No. You believe it works, but it doesn’t. It is simply impossible - IF it is the same as the code you posted. Because states(‘device_wifi_location_presence’) will ALWAYS return an empty string. It will never reference the state of the sensor you created before this one. it MUST use sensor.

This sensor is ALWAYS being updated based solely on the device_tracker.

1 Like

Also, this is not a valid entity ID:

image

Hi all,
I figured out at the end, there where 2 issues:

  1. as notified by most of you the missing of “sensor.” before the strings
  2. there was a typo with if and elif

Now it seems to work, I see all the sensors created. I’ll check also if the logic for the location is better.

Thanks to all for the help

1 Like