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:
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 }}’
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.
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
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…
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??
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?