Read Sensor State from one HA instance to another

I have a rest_command currently that on one Home Assistant instance runs via an automation every hour to publish a sensor value to another home assistant instance. This works well however, if the ‘master’ home assistant instance restarts then it can take up to an hour before the ‘slave’ instance runs the automation to push the sensor to the master again.

So I want to write a rest sensor which can pull the sensor value from the slave on the master.
So far I have:

sensor:
  - platform: rest
    name: my_rest_command
    resource: https://domain.duckdns.org:8123/api/states/sensor.level
    method: GET
    headers: 
      Authorization: 'Bearer xxxxxxtokenxxxxxx'
      content-type: 'application/json'

So there is a sensor on the ‘slave’ called sensor.level and it has a state.

When I run the rest command on the master I get this in the logs:

2019-11-20 16:37:55 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 408, in _async_add_entity
    await entity.async_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 275, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 394, in _async_write_ha_state
    self.entity_id, state, attr, self.force_update, self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 988, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 733, in __init__
    ).format(entity_id)
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.my_rest_command. State max length is 255 characters.

The state of the sensor in the ‘slave’ is no longer than 10 characters.

What am I missing here?

Ah I just ran it as a curl command and got a shit load of text so I think I need:
value_template: ‘{{ value_json.state }}’

Error:

2019-11-20 16:54:57 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 275, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 394, in _async_write_ha_state
    self.entity_id, state, attr, self.force_update, self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 988, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 733, in __init__
    ).format(entity_id)
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.my_rest_command. State max length is 255 characters.

But the sensor is being created anyway…

the curl command returns this:

{ 
   "attributes":{ 
      "friendly_name":"Sensor Level",
      "icon":"mdi:shield-home"
   },
   "context":{ 
      "id":"720550a3c5d54d9da8caa4a582dc0c55",
      "parent_id":null,
      "user_id":null
   },
   "entity_id":"sensor.level",
   "last_changed":"2019-11-20T05:26:26.857895+00:00",
   "last_updated":"2019-11-20T05:26:26.857895+00:00",
   "state":"v7.0.0.5"
}

How do I make it not read in the rest?

You want to extract “state”? Maybe this works:

sensor:
  - platform: rest
    name: my_rest_command
    resource: https://domain.duckdns.org:8123/api/states/sensor.level
    method: GET
    headers: 
      Authorization: 'Bearer xxxxxxtokenxxxxxx'
      content-type: 'application/json'
    value_template: "{{ value_json.state }}"

as per first post I already added the value template like that.
It gives the 255 character limit error repeatedly in the log and does give the sensor a state but I can’t have it flooding the logs

Sorry, I didn’t read that part. Maybe you could try to add “json_attributes” and choose only “entity_id” as attribute.

sensor:
  - platform: rest
    name: my_rest_command
    resource: https://domain.duckdns.org:8123/api/states/sensor.level
    method: GET
    headers: 
      Authorization: 'Bearer xxxxxxtokenxxxxxx'
      content-type: 'application/json'
    value_template: "{{ value_json.state }}"
    json_attributes:
      - entity_id

I had tried that before. Tried again. It does create the sensor but also the continuous error messages about 255 character limit

Hmm, sorry I see that I’m not really helpful.
Maybe you could try with a command line sensor instead?

Stupid thing is I am following this:


It just doesn’t cater for the sensor having a longer length

Have a look at Home Assistant Remote - you can configure it to just pull a single entity, everything, or something in between.

how odd… what is the state of the original sensor? Is it more than 255 char?

as an example, I use this:

  - platform: rest
    name: Hassio Rpi4 config
    resource: !secret resource_hassio_rpi4_config
#    authentication: basic
    value_template: >
      {{ value_json.version }}
    json_attributes:
      - components
      - unit_system
      - config_dir
      - version
    headers:
      Content-Type: application/json
      Authorization: !secret api_bearer_token
      User-Agent: Home Assistant REST sensor

which takes care all info is loaded into the attributes (which can be more than 255 chars) and only shows the version as state…

next to @Tinkerer 's suggestion (thanks, hadn’t seen that before!) you can also relay state from one system to another through mqtt, did you check that? You can be very specific what to send, as to prevent any 255 char limit.

Thanks. I posted the output when I do the curl command up above. The state is ~10 characters…
I had thought of using MQTT but that’s just an extra level of complication and I should be able to use the rest sensor…

Talk about using a sledge hammer to crack a nut… Thanks.

understand, and it makes it unexpected, the state being only

"state":"v7.0.0.5"

you could always try to truncate it somehow? Did below template on a sensor to have it at least appear in the frontend without the errors, knowing it might be cutoff. Which in your case wouldn’t be the case anyway:

        {{list if list|count < 255 else
          list|replace('input','inp')|truncate(255,true) }}

The really stupid thing is the state is being populated with the correct value… it’s just the constant errors about the json being longer than 255 characters. I also did try a resource_template without luck.

yea, really annoying.
So, to be sure on this side, your rest sensor is creating the correct sensor, with a correct state, and is displayed in the frontend/state page, but also generates the error about 255, which it hasn’t??

Correct. And the error comes up every 30 seconds it seems…

sure, thats the default update time.
I’d issue a bug on this, can’t explain what’s happening, and in fact, it shouldn’t…

And today it’s working. How odd is that!

ok so I hate to complain about this but it is updating every 30 seconds… and the remote HA is on a 4G network and it’s going to cost a fortune. So I was looking at switching to a rest command that I can just execute via an automation. That works… well there is no error… but where does the data go from the ‘get’ command? I can execute the rest command from services but it doesn’t create a sensor… is it supposed to? Or is there any way I can make the rest sensor only update on HA start?

I use

    scan_interval: 86400

on the rest sensor making it update once a day :wink: Or on restart of course.

2 Likes