Template Sensor Showing Unknown

I have a Ring doorbell configured and running with the Ring integration. I’m trying to get one of the states exposed as a template sensor and struggling for some reason. The doorbell’s attributes look something like this

access_token: xyz
attribution: Data provided by Ring.com
video_url: https://share-service-download-bucket.s3.amazonaws.com/abc
last_video_id: epcoh_time
friendly_name: Front Door
entity_picture: /api/camera_proxy/camera.front_door?token=mytoken
supported_features: 0

The attribute I’m after is video_url, so in the developer tab under states I figured out that {{ state_attr('camera.front_door', 'video_url') }} gives me what I want. I took that and created a template sensor that looks like this, modeled off the example in the docs

  - platform: template
        friendly_name: 'Front Door Video URL'
        value_template: "{{ state_attr('camera.front_door', 'video_url') }}"
          - camera.front_door

When I restart the state just returns as Unknown and I can’t figure out why. Any pointers?

Template sensors only change their state if one of the configured entity_id’s changes their state.

Have you tried it with homeassistant.update_entity ?
1 Like

I had not previously, but did so just now and still shows as unknown. Also verified that camera.front_door had a valid state (the attribute was there and populated) before running it.

Does the sensor change the state when you click the camera in dev tools/states (not the more-info, the link), change something in the state or the attributes an click ‘SET STATE’?

You’re onto something! I tried just adding an attribute and setting it, so I went from

access_token: <removed>
attribution: Data provided by Ring.com
video_url: <removed>
last_video_id: <removed>
friendly_name: Front Door
entity_picture: /api/camera_proxy/camera.front_door?token=<removed>
supported_features: 0


access_token: <removed>
attribution: Data provided by Ring.com
video_url: <removed>
last_video_id: <removed>
friendly_name: Front Door
entity_picture: /api/camera_proxy/camera.front_door?token=<removed>
supported_features: 0
new_feature: test

When I did that the state stayed as unknown. But when I changed the actual video URL from what was there to, quite literally, bbbbb the template sensor updated to be bbbbb.

The URL that’s present is quite long, and does have a number of special (albeit URL safe) characters. Is it possible that something is making the template to be unhappy? Is there a way to make sure that everything is being escaped?

A state can’t be longer than 255 chars, but that should raise an error in the logs, or something has changed in one of the last updates.

I’d checked the logs, but never expanded it to the full log (only looked at the summary). I did however do that now and sure enough I found the error below. Guess that answers my question, thanks for helping me work through that. Time to switch tactics for this automation trigger.

2020-08-03 15:37:10 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 277, in async_update_ha_state
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 402, in _async_write_ha_state
  File "/usr/src/homeassistant/homeassistant/core.py", line 1019, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 766, in __init__
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.front_door_video_url. State max length is 255 characters.

If you want to trigger an automation with the video_url attribute, why not use a template trigger?

EDIT: Forget the template trigger, that should evaluate to true.
But you can use the state trigger with the camera as entity_id and check the video_url has changed with a template condition and the Trigger state object.

That was initially the road I’d gone down, and in the process found this thread which was someone essentially trying to do the same thing and seeing the same issues I was (only fired on the initial boot). I did however notice this reply which seems like it’ll work perfectly. Does that seem reasonable or am I coming at this a bad way?

Your edit appeared as soon as I hit save. I think we arrived at the same thing :slight_smile:

Hi. I didn’t read this whole topic, or re-read the whole topic referenced, but I can tell you one thing – that post of mine that you linked is WAY OLD!! :smile:

Today I would recommend the following for the condition:

      condition: template
      value_template: >
        {{ trigger.from_state is not none and
           trigger.to_state is not none and
           trigger.to_state.attributes.temperature !=
           trigger.from_state.attributes.temperature }}

Old yes, but did the trick! Appreciate the update though, I’ll add in those other conditions to control for no state, that’s a good call.