Rest sensor is unknown every 2 minutes

Hi!
I have a problem where my rest sensor state is displaying unknown for 1 minutes and correct 1 minute

This is my code, i need help or direction on what i’m doing wrong. first time it gets called it’s no error in the log. second time it gets length error

- platform: rest
  resource: https://api.tibber.com/v1-beta/gql
  method: POST
  force_update: true
  timeout: 30
  scan_interval: 60  # Poll every hour
  headers:
    Authorization: "Bearer apitoken"
    Content-Type: "application/json"
  payload: '{"query": "{ viewer { homes { currentSubscription { priceInfo { today { total startsAt } tomorrow { total startsAt } } } } } }"}'
  json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo"
  json_attributes:
    - today
    - tomorrow
  value_template: >
    {% set today_totals = state_attr('sensor.rest_sensor', 'today') %}
    {% set tomorrow_totals = state_attr('sensor.rest_sensor', 'tomorrow') %}
    {% if not today_totals%}
      Waiting for data...
    {% else %}
      Today:
      {% for item in today_totals %}
        - {{ item.total }}
      {% endfor %}
    {% endif %}
    
    {% if not tomorrow_totals%}
      Waiting for data...
    {% else %}
      Today:
      {% for item in tomorrow_totals %}
        - {{ item.total }}
      {% endfor %}
    {% endif %}
2024-07-17 20:14:12.673 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://api.tibber.com/v1-beta/gql
2024-07-17 20:14:12.860 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"data":{"viewer":{"homes":[{"currentSubscription":{"priceInfo":{"today":[{"total":0.4036,"startsAt":"2024-07-17T00:00:00.000+02:00"},{"total":0.4009,"startsAt":"2024-07-17T01:00:00.000+02:00"},{"total":0.3971,"startsAt":"2024-07-17T02:00:00.000+02:00"},{"total":0.389,"startsAt":"2024-07-17T03:00:00.000+02:00"},{"total":0.3809,"startsAt":"2024-07-17T04:00:00.000+02:00"},{"total":0.3756,"startsAt":"2024-07-17T05:00:00.000+02:00"},{"total":0.3982,"startsAt":"2024-07-17T06:00:00.000+02:00"},{"total":0.4323,"startsAt":"2024-07-17T07:00:00.000+02:00"},{"total":0.4411,"startsAt":"2024-07-17T08:00:00.000+02:00"},{"total":0.4313,"startsAt":"2024-07-17T09:00:00.000+02:00"},{"total":0.3966,"startsAt":"2024-07-17T10:00:00.000+02:00"},{"total":0.3691,"startsAt":"2024-07-17T11:00:00.000+02:00"},{"total":0.3794,"startsAt":"2024-07-17T12:00:00.000+02:00"},{"total":0.3574,"startsAt":"2024-07-17T13:00:00.000+02:00"},{"total":0.2524,"startsAt":"2024-07-17T14:00:00.000+02:00"},{"total":0.1993,"startsAt":"2024-07-17T15:00:00.000+02:00"},{"total":0.2261,"startsAt":"2024-07-17T16:00:00.000+02:00"},{"total":0.2682,"startsAt":"2024-07-17T17:00:00.000+02:00"},{"total":0.2799,"startsAt":"2024-07-17T18:00:00.000+02:00"},{"total":0.2988,"startsAt":"2024-07-17T19:00:00.000+02:00"},{"total":0.3001,"startsAt":"2024-07-17T20:00:00.000+02:00"},{"total":0.2942,"startsAt":"2024-07-17T21:00:00.000+02:00"},{"total":0.3091,"startsAt":"2024-07-17T22:00:00.000+02:00"},{"total":0.2689,"startsAt":"2024-07-17T23:00:00.000+02:00"}]}}}]}}}

2024-07-17 20:15:12.862 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://api.tibber.com/v1-beta/gql
2024-07-17 20:15:13.063 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"data":{"viewer":{"homes":[{"currentSubscription":{"priceInfo":{"today":[{"total":0.4036,"startsAt":"2024-07-17T00:00:00.000+02:00"},{"total":0.4009,"startsAt":"2024-07-17T01:00:00.000+02:00"},{"total":0.3971,"startsAt":"2024-07-17T02:00:00.000+02:00"},{"total":0.389,"startsAt":"2024-07-17T03:00:00.000+02:00"},{"total":0.3809,"startsAt":"2024-07-17T04:00:00.000+02:00"},{"total":0.3756,"startsAt":"2024-07-17T05:00:00.000+02:00"},{"total":0.3982,"startsAt":"2024-07-17T06:00:00.000+02:00"},{"total":0.4323,"startsAt":"2024-07-17T07:00:00.000+02:00"},{"total":0.4411,"startsAt":"2024-07-17T08:00:00.000+02:00"},{"total":0.4313,"startsAt":"2024-07-17T09:00:00.000+02:00"},{"total":0.3966,"startsAt":"2024-07-17T10:00:00.000+02:00"},{"total":0.3691,"startsAt":"2024-07-17T11:00:00.000+02:00"},{"total":0.3794,"startsAt":"2024-07-17T12:00:00.000+02:00"},{"total":0.3574,"startsAt":"2024-07-17T13:00:00.000+02:00"},{"total":0.2524,"startsAt":"2024-07-17T14:00:00.000+02:00"},{"total":0.1993,"startsAt":"2024-07-17T15:00:00.000+02:00"},{"total":0.2261,"startsAt":"2024-07-17T16:00:00.000+02:00"},{"total":0.2682,"startsAt":"2024-07-17T17:00:00.000+02:00"},{"total":0.2799,"startsAt":"2024-07-17T18:00:00.000+02:00"},{"total":0.2988,"startsAt":"2024-07-17T19:00:00.000+02:00"},{"total":0.3001,"startsAt":"2024-07-17T20:00:00.000+02:00"},{"total":0.2942,"startsAt":"2024-07-17T21:00:00.000+02:00"},{"total":0.3091,"startsAt":"2024-07-17T22:00:00.000+02:00"},{"total":0.2689,"startsAt":"2024-07-17T23:00:00.000+02:00"}]}}}]}}}

2024-07-17 20:15:13.064 ERROR (MainThread) [homeassistant.helpers.entity] Failed to set state for sensor.rest_sensor, fall back to unknown
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1205, in _async_write_ha_state
    hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 2302, in async_set
    state = State(
            ^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1773, in __init__
    validate_state(state)
  File "/usr/src/homeassistant/homeassistant/core.py", line 239, in validate_state
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state with length 389. State max length is 255 characters.

Every entity’s state property can only hold a maximum of 255 characters. Sometimes your REST Sensor’s template produces a value that exceeds the limit.

Ensure your template never produces a string whose length exceeds 255 characters.

If you must store a longer value, then you’ll have use attributes, not state, because it can hold about 16K.

1 Like

I also see that.
The comment says every hour, but I would bet the 30 & 60 are actually seconds. Only mention in case you think that is minutes.

Also the problem you list

shows that this is looking every minute. I would suggest a longer time if your needs can allow it.

That is nice to know, thank you.
This is my code now that works without every 2 minutes the attributes disapear
The only thing that i can’t seem to grasp is how you set a state in the rest sensor and have a way to display attributes aswell

- platform: rest
  resource: https://api.tibber.com/v1-beta/gql
  method: POST
  force_update: true
  timeout: 30
  scan_interval: 60 # Poll every hour
  headers:
    Authorization: !secret tibberapikey
    Content-Type: "application/json"
  payload: '{"query": "{ viewer { homes { currentSubscription { priceInfo { today { total startsAt } tomorrow { total startsAt } } } } } }"}'
  json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo"
  json_attributes:
    - today
    - tomorrow
  value_template: >
    {{value_json.today[0].total}}
    {{value_json.tomorrow[0].total}}
    {% set dataToday = value_json.today[0].total %}
    {% set dataTomorrow = value_json.tomorrow[0].total %}
    {% if dataToday and dataTomorrow %}
      OK
    {% elif not dataTomorrow and dataToday %}
      OK, no tomorrow data yet
    {% else %}
      no data
    {% endif %}


- platform: template
  sensors:
    tibber_today:
      friendly_name: "Tibber Price Data Today Tomorrow"
      value_template: >
        {% set dataToday = state_attr('sensor.rest_sensor', 'today') %}
        {% set dataTomorrow = state_attr('sensor.rest_sensor', 'tomorrow') %}
        {% if dataToday and dataTomorrow %}
          OK
        {% elif not dataTomorrow and dataToday %}
          OK, no tomorrow data yet
        {% else %}
          no data
        {% endif %}
      attribute_templates:
        today: >
          {% set data = state_attr('sensor.rest_sensor', 'today') %}
          {% if data %}
            [
            {% for item in data %}
              ["{{ item.startsAt }}",{{ item.total }}]{% if not loop.last %},{% endif %}
            {% endfor %}
            ]
          {% else %}
            []
          {% endif %}
        tomorrow: >
          {% set data = state_attr('sensor.rest_sensor', 'tomorrow') %}
          {% if data %}
            [
            {% for item in data %}
              ["{{ item.startsAt }}",{{ item.total }}]{% if not loop.last %},{% endif %}
            {% endfor %}
            ]
          {% else %}
            []
          {% endif %}

I’m sorry that i diden’t mention that i have set the time to 60 seconds for debugging purposes. It gonna be set to 3600 seconds later.