Trying to use platform rest sensor(s) to pull values for a solar inverter only between sunrise and sunset

Trying to use platform rest sensor(s)s to pull values for a solar inverter only between sunrise and sunset.

I thought this should be a fairly common thing to do, but I have not found it in my searching.

My local webserver only polls the inverter communications C++ linux executable during sunrise to sunset, so there is no point HA polling outside those times.
Most values go to zero after sunset, and the inverter shuts off also when there is no sun on the panels to power it.

I get values for my older solar inverter from my local webserver from a json php ( https://xxx.xxx.xxx.xxx/x/x/programlive.php?invtnum=1 )

It returns values (e.g. some of them listed below) during daylight:

{"SDTE":<RETRACTED>,"I1V":281.9,"I1A":5.3,"I1P":1495,"I2V":308.2,"I2A":5.4,"I2P":1658,"I3V":0,"I3A":0,"I3P":0,"I4V":0,"I4A":0,"I4P":0,"G1V":243.6,"G1A":12.2,"G1P":2953,"G2V":0,<LOTS MORE RETRACTED>,"KWHT":50990.507,"timestamp":"26\/01\/2023 15:56:21",<RETRACTED>}

At night:

{"SDTE":<RETRACTED>,"I1V":0,"I1A":0,"I1P":0,"I2V":0,"I2A":0,"I2P":0,"I3V":0,"I3A":0,"I3P":0,"I4V":0,"I4A":0,"I4P":0,"G1V":0,"G1A":0,"G1P":0,"G2V":0,"G2A":0,"G2P":0,"G3V":0,"G3A":0,"G3P":0,"FRQ":0,"EFF":0,"INVT":0,"BOOT":0,"SSR":0,"PMAXOTD":0,"PMAXOTDTIME":0,"timestamp":"25\/01\/2023 23:40:25","riso":0,"ileak":0,"awdate":"--:--"}

All values go to 0 after sunset except timestamp and KWHT

KWHT is the inverter total kWh produced (forever increasing so never shows zero) and does not show in the json at all after sunset (which is what I use in the HA energy dashboard).

HA OS 9.4 / Core 2023.1.7 with all latest updates installed.

config.yaml:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# My Modifications
# Groups
group: !include groups.yaml
# Sensors
sensor: !include sensors.yaml
# Templates
template: !include templates.yaml

I currently have the sensors setup in:

sensors.yaml

#sensor:

# Solar Monitoring (West)

  - platform: rest
    name: West Solar kWh Total
    resource: !secret solar_west_res
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: kWh
    value_template: "{{ value_json.KWHT }}"

  - platform: rest
    name: West Solar Power
    resource: !secret solar_west_res
    device_class: power
    unit_of_measurement: W
    value_template: "{{ value_json.G1P }}"
    force_update: true

  - platform: rest
    name: West Solar Amp
    resource: !secret solar_west_res
    unit_of_measurement: A
    value_template: "{{ value_json.G1A }}"

  - platform: rest
    name: West Solar Max Output
    resource: !secret solar_west_res
    unit_of_measurement: W
    value_template: "{{ value_json.PMAXOTD }}"

  - platform: rest
    name: West Solar Max Output Time
    resource: !secret solar_west_res
    value_template: "{{ value_json.PMAXOTDTIME }}"


# The remaining sensors are usually below here but cut to keep it short and readable

I should probably convert these to the modern style, as all my other templates and sensors are.

And also make them all a single call to be more efficient as well!

A few failed attempts below:

# Between Sunrise and Sunset
condition:
  - condition: sun
    after: sunrise
    before: sunset

  - platform: rest
    name: West Solar kWh Total
    resource: !secret solar_west_res
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: kWh
    value_template: "{{ value_json.KWHT }}"

  - platform: rest
    resource: !secret solar_west_res
    name: West Solar Power
    device_class: power
    unit_of_measurement: W
    value_template: "{{ value_json.G1P }}"
    force_update: true

Also:

  - platform: rest
    condition:
    - condition: sun
      after: sunrise
      before: sunset
      resource: !secret solar_west_res
      name: West Solar Power
      device_class: power
      unit_of_measurement: W
      value_template: "{{ value_json.G1P }}"
      force_update: true

Also:

  - platform: rest
    resource: !secret solar_west_res
    name: West Solar Power
    device_class: power
    unit_of_measurement: W
    condition:
    - condition: sun
      after: sunrise
      before: sunset
      value_template: "{{ value_json.G1P }}"
      force_update: true

Errors:

Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 26). 
'Invalid config for [sensor.rest]: must contain at least one of resource, resource_template.. Got OrderedDict([('platform', 'rest'), ('condition', [OrderedDict([('condition', 'sun'), ('after', 'sunrise'), ('before', 'sunset'), ('resource', '** REDACTED **'), ('name', 'West Solar Power'), ('device_class', 'power'), ('unit_of_measurement', 'W'), ('value_template', '{{ value_json.G1P }}'), ('force_update', True)])])]). (See ?, line ?).'

You can’t just randomly mix conditions into a sensor definition. Set a long scan_interval and update it using an automation: perhaps a time_pattern trigger with a sun status condition. See my example here:

Ignore all the details about the printer, but note that by updating one of the rest sensors pulling from that resource, they all get updated because I’m using the RESTful integration (so rest » sensor rather than sensor » rest). You should also do that as you’re pulling a few sensors from the same resource.

rest:
  - resource: !secret solar_west_res
    scan_interval: 86400
    sensor:
      - name: "West Solar Power"
        unique_id: 2fd38419-64ce-4560-9968-c3c255431d3f
        device_class: power
        unit_of_measurement: W
        value_template: "{{ value_json.G1P }}"

      - name: "West Solar kWh Total"
        unique_id: 76b77aeb-cd2f-4540-ac6a-3eec2c903212
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: kWh
        value_template: "{{ value_json.KWHT }}"

Yes, as shown above I soon worked out that I couldn’t mix conditions into the sensor definitions.

Thanks for pointing me in the right direction, all the sensors updating at once is desired in this case, but I’ll keep it in mind for any other times I need to use something like that.
I will be able to get it to work now with an automation, in my mind I knew it should be able to be done with an automation, it just didn’t seem like the easy / most logical way to do it.

I was also surprised it wasn’t more common and I must have been missing something simple?

Cheers