Template sensor not updating as expected

Hello,

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:

- trigger:
    - platform: state
      entity_id: sensor.solax_x1
      to: "OK"

  sensor:
    - name: "Solax Energy today"
      unit_of_measurement: "kWh"
      state: "{{ state_attr('sensor.solax_x1', 'Data')[13] / 10 }}"
      device_class: energy
      state_class: total_increasing
      icon: mdi:solar-panel

    - name: "Solax Energy Total"
      unit_of_measurement: "kWh"
      state: "{{ state_attr('sensor.solax_x1', 'Data')[11] / 10 }}"
      device_class: energy
      state_class: total_increasing
      icon: mdi:chart-line

    - name: "Solax AC Frequency"
      state: "{{ state_attr('sensor.solax_x1', 'Data')[9]  | float / 100 }}"
      unit_of_measurement: "Hz"
      device_class: "frequency"
      icon: mdi:music-clef-treble

    - name: "Solax AC Power"
      state: "{{ state_attr('sensor.solax_x1', 'Data')[7] }}"
      unit_of_measurement: "W"
      device_class: "power"  
      icon: mdi:power-socket-de

I have two issues at the moment.

The first one:

  • When I reboot HA and the solax_1 is unavailable Solax 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

I would appreciate your thoughts.

Did you check the JSON content in the problematic cases?
Are there any messages in the log related to this?

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?

You could as well feed the sensors directly in the rest call.

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.

@eXtatic thanks for your input.

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.

Thanks @Troon for your reply.

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.

How often do these warnings occur?

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.