REST sensor pv forecast postprocessing issues

@Troon : Two additional question:
1.) I recieve the data from the API in UTC and depending on the summer/winter time we have +1 or +2 in austria… how can I take this into account?

2.) I just found out that the sensor value does not get updated each hour. It just gets updated at startup of HA. How can I change this?

  1. You just need to set time_now such that it gives you the string value you need for comparing against the list. Have a look at the many options available for manipulating time here: Templating - Home Assistant . Perhaps utcnow() will help.

  2. What is only getting updated at restart? You have a 1h10s scan_interval on the rest sensor in your original post.

Ad1: Is there a way to manipulate the attribute list date and add there the hours by 1h (or 2h)? Otherwise I will have to do the proposed solution by you. :slight_smile:

Ad2: Yes you are right, the values get updated each 1h10s (due to API limitation)

via the rest API. What I would like to have is that the values in the attribute list get updated each 1h10s, but the value of the sensor should get checked and (if needed) updated each minute. (In the Image you can see that I last restarted at 12:28 and since then each 3610s the values get updated, even there are new values in the attribute list for each full hour.) I hope I could explain it clear enough ^^

Thank you in advance!

  1. Not easily. Much easier to adjust the comparison time.

  2. In that case, you may be better off with a separate template sensor reading the value from the rest sensor. Something like this (basically the same template but reading the attribute rather than the response; need to do the timezone adjustment here too):

template:
  - sensor:
      - name: Solar Forecast East current
        state: >
          {% set time_now = now().strftime('%Y-%m-%d %H:%M:%S') %}
          {% set tkey = state_attr('sensor.solar_forecast_east_watt', 'watts')
                        |sort|select('le', time_now)|list|last %}
          {{ state_attr('sensor.solar_forecast_east_watt', 'watts')[tkey] }}

Thank you for your help. there was an error because the template sensor uses state instead of value_template. Aft the change it seems to work ( I will have to wait until tomorrow when there is light again - at the moment the value is always 0 :wink:

just for other people who might be interested here are the example sensors:

rest:
  - scan_interval: 3610
    resource: https://api.forecast.solar/estimate/47.00000/15.00000/45/-10/10
    method: GET
    sensor:
      - name: "Solar Forecast Watt South Raw"
        json_attributes_path: "$.result"
        json_attributes:
          - watts
        value_template: 'OK'
      - name: "Solar Forecast Watt Hours South Raw"
        json_attributes_path: "$.result"
        json_attributes:
          - watt_hours
        value_template: 'OK'
      - name: "Solar Forecast Watt Hours Day South Raw"
        json_attributes_path: "$.result"
        json_attributes:
          - watt_hours_day
        value_template: 'OK'
      - name: "Solar Forecast Watt Hours Period South Raw"
        json_attributes_path: "$.result"
        json_attributes:
          - watt_hours_period
        value_template: 'OK'

template:
    - sensor:
          - name: "Solar Forecast Watt South"
            unit_of_measurement: "W"
            device_class: power
            state_class: measurement
            unique_id: solar_forecast_watt_south
            availability: "{{state_attr('sensor.solar_forecast_watt_south_raw', 'watts')[state_attr('sensor.solar_forecast_watt_south_raw', 'watts')|sort|select('le', now().strftime('%Y-%m-%d %H:%M:%S'))|list|last] |float |is_number }}"  
            state: >
                {% set time_now = now().strftime('%Y-%m-%d %H:%M:%S') %}
                {% set tkey = state_attr('sensor.solar_forecast_watt_south_raw', 'watts')
                              |sort|select('le', time_now)|list|last %}
                {{ state_attr('sensor.solar_forecast_watt_south_raw', 'watts')[tkey] | float }}

so next step is to shift the time difference from UTC to UTC+1/UTC+2…will take a look into that :wink:

:disappointed: sorry, fixed in prior post. Well done for working that out. :clap:

I see you have things solved. FYI, you would add the flag --null-input to the JQ command which is the cause of the first line missing. In JQ PLayground:

@kbrown01 Thanks for this info :slight_smile:

Can somebody explain to me why the conversion from Wh to kWh is not working? After restarting HA the Values stay in Wh…

template:
          - name: "Solar Forecast Watt Hours South"
            unit_of_measurement: "kWh"
            device_class: energy
            state_class: measurement
            unique_id: solar_forecast_watt_hours_south
            availability: "{{state_attr('sensor.solar_forecast_watt_hours_south_raw', 'watt_hours')[state_attr('sensor.solar_forecast_watt_hours_south_raw', 'watt_hours')|sort|select('le', now().strftime('%Y-%m-%d %H:%M:%S'))|list|last] |float |is_number }}"  
            state: >
                {% set time_now = now().strftime('%Y-%m-%d %H:%M:%S') %}
                {% set tkey = state_attr('sensor.solar_forecast_watt_hours_south_raw', 'watt_hours')
                              |sort|select('le', time_now)|list|last %}
                {{ state_attr('sensor.solar_forecast_watt_hours_south_raw', 'watt_hours')[tkey] /1000 | float}}

If I test it in the dev-tools it seems to work like expected.

Furthermore I can see that the value of the sensor gets updated, but does not get divided by 1000 ( and also the unit did not change to kWh:
image

What does the States page show? This might just be due to the history having two different units for the same sensor — try restricting the graph to just the period after when you changed the sensor definition and reloaded. If that is the case, it’ll fix itself once the old definition has “scrolled left” enough in time.

no, it seems to be something different… :frowning: if I look in the dev tools I see the following:

I assume there’s a - sensor: there that you failed to paste in?

Try changing the unique_id: I always use random UUIDs from here.

Then reload template entities, no need to restart. Developer Tools / YAML / TEMPLATE ENTITIES.

yes, I forgot to paste the -sensor …

thank you for this homepage - I now exchanged the unique_ids like you suggested and it gets displayed in kWh… thank you very much :smiley:

I now look into the time shifting :slight_smile:

I used your code with my position, but can you please explain …

… whats the difference between - label: watt_hours, and label: watt_hours_period