To integrate my solax X1 inverter I 've created the following sensor:
- platform: rest
authentication: digest
scan_interval: 5
resource: http://192.168.1.xx/
payload: "optType=ReadRealTimeData&pwd=xxxxxxxxx"
method: POST
headers:
X-Forwarded-For: 5.8.8.8
name: "solax_x1"
json_attributes:
- type
- SN
- ver
- Data
- Information
value_template: >-
{% if value_json.Data | length == 100 %}
OK
{% else %}
NOT OK
{% endif %}
Then I’ve created a templated trigger-based sensor that extracts data from the JSON attributte:
When I reboot HA and the solax_1 is unavailableSolax Energy Total becomes unknown. The weird thing is that the others seem to work as expected.
The second:
When the solax_x1 becomes available again in the morning it takes sometime to trigger the template sensor. I’ve tried to manually set it to NOT OK and as soon as it became OK again it triggered the template sensor immediately. I was expecting to happen the same when it switched from unavailable to OK
You’re scanning the URL every 5 seconds, which seems a bit too frequent.
Your template sensors will only update when the state of the rest sensor changes from something that isn’t OK to OK, so if it successfully updates its attributes the state will not change and the templates will not update. It won’t go unavailable in between successful updates.
Why not make the template sensors non-triggered? Just remove the trigger section completely, then they will update whenever the rest sensor updates.
Did you check the JSON content in the problematic cases?
JSON length is always 100 and always brings the expected data. But when there is no light the sensor becomes unavailable. That’s why I have a trigger-based template sensor to deal with this. I wll look to data attribute only when sensor is available.
Are there any messages in the log related to this?
Only when sensor becomes unavailable:
2023-10-23 11:01:19.878 WARNING (MainThread) [homeassistant.components.sensor] Updating rest sensor took longer than the scheduled update interval 0:00:05
2023-10-23 11:01:19.880 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.solax_x1 is taking over 10 seconds
2023-10-23 11:01:19.895 ERROR (MainThread) [homeassistant.components.rest.data] Timeout while fetching data: http://192.168.1.10/
2023-10-23 11:01:19.901 WARNING (MainThread) [homeassistant.components.rest.util] Empty reply found when expecting JSON data
Out of curiosity, why are you putting the JSON data into attributes every 5 seconds but only extract them into sensors depending on JSON length?
I want to extract data every time the data attribute is updated. As I said length is always 100.
You could as well feed the sensors directly in the rest call.
I could do that but I want to deal with unavailability of the sensor.
You’re scanning the URL every 5 seconds, which seems a bit too frequent.
Perhaps. I want to see real-time updates of power. But I also wonder if 5 seconds was too much.
Your template sensors will only update when the state of the rest sensor changes from something that isn’t OK to OK , so if it successfully updates its attributes the state will not change and the templates will not update. It won’t go unavailable in between successful updates.
I want to update every time the attribute changes. And in fact it is updating. I all works fine except when it becomes unavailable. In the morning it does start updating again.
Why not make the template sensors non-triggered? Just remove the trigger section completely, then they will update whenever the rest sensor updates.
Because I need to deal with unavailability of the sensor. When it goes to unavailable I want my template sensors to keep the previous value instead of going to unavailable.
2023-10-23 11:01:19.878 WARNING (MainThread) [homeassistant.components.sensor] Updating rest sensor took longer than the scheduled update interval 0:00:05
2023-10-23 11:01:19.880 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.solax_x1 is taking over 10 seconds
2023-10-23 11:01:19.895 ERROR (MainThread) [homeassistant.components.rest.data] Timeout while fetching data: http://192.168.1.10/
2023-10-23 11:01:19.901 WARNING (MainThread) [homeassistant.components.rest.util] Empty reply found when expecting JSON data
As already mentioned by Troon you might be polling too fast.
You’re polling every 5 seconds but you did not lower the timeout which is 10 seconds by default.
Timeout should be set lower than scan_interval.