Need help storing URL for use in generic IP camera (over 255 chars)

I have an IP cloud camera that lets you fetch a snapshot URL via API (REST). I am trying to use the RESTful sensor to get and store this URL each minute, but unfortunately the URL returned is longer than 255 characters, and therefore it is not storing. Instead I get this error:

Invalid state encountered for entity id: sensor.cam_1_url. State max length is 255 characters.

Is there some other method I can use to store this URL to be used as the still_image_url: in the generic camera platform? Here is what I currently have setup:

sensor:
  - platform: rest
      name: Cam 1 URL
      resource: https://192.168.1.68/api/v0/CAM1/snapshot
      method: GET
      scan_interval: 60
      authentication: basic
      headers:
        API-Key: !secret cam_api_key
        value_template: '{{value_json.url}}'

camera:
  - platform: generic
    name: Doggie Camera
    still_image_url: '{{ states("sensor.cam_1_url") }}'

Attributes can be longer than 255 characters; perhaps you could put the URL into an attribute instead, and some dummy value for the actual sensor? Or some other response (possibly even a truncated string) that you can ignore.

You can use the json_attributes thing to capture this other info.

Take a look at the file I attached to the first post in this thread
Monitor your Generac generator with Home Assistant for an example. Just an excerpt that might give you a sense of this:

  - platform: rest
    name: genmon_status_json
    hidden: true
    resource: http://generator.local:8000/cmd/status_json
    value_template: '{{ value_json["Status"]["Engine"]["Engine State"] }}'
    friendly_name: "Generator State"
    json_attributes:
      - "Status"
      - "Line State"
      - "Last Log Entries"
      - "Time"
    method: GET
    scan_interval: 65

and the template sensors that look like this:

  - platform: template
    sensors:

      # outage related sensors
      genmon_outage_status:
        entity_id: sensor.genmon_outage_json
        friendly_name: 'Generator Status'
        value_template: '{{ states.sensor.genmon_outage_json.attributes["Outage"]["Status"] }}'

which access the attributes that were previously squirreled away.

Perhaps something like this could be adapted to your problem?

2 Likes

You’re a hero, thanks @lmamakos! I had read about json_attributes being able to store >255 characters but I didn’t fully grasp it until I looked at your sample file. For anyone that is running into a similar challenge, here’s what I ended up with that worked:

sensor:
  - platform: rest
      name: Cam 1 URL
      resource: https://192.168.1.68/api/v0/cam1/snapshot
      method: GET
      scan_interval: 60
      authentication: basic
      headers:
        API-Key: !secret cam_api_key
      value_template: '{{value_json.timestamp}}'
      json_attributes:
         - "url"

camera:
  - platform: generic
    name: Doggie Camera
    still_image_url: '{{ states.sensor.cam_1_url.attributes["url"] }}'

Glad to hear that worked for you! I was also surprised by the state length limitation, and stumbled over the json_attributes thing somewhere along the way…