Turning on multiple heaters when inverter exporting to grid

Hi, during cloudy days my solar system generation is all over the place, and due to not getting any feed in tarrifs, I would rather use all of the solar I produce. So HA is all set up and I can read the sensor for grid usage, but I can’t seem to even get one switch to work when the solar export is over a certain number, let alone do what I want. The idea is, two 2000w heaters and one 1000w heater.

So when export is >1000 turn on switch bedroom (1000w heater)
if export is another >1000, turn off switch.bedroom, turn on switch.living1 (2000w)
if export is another >1000, turn on switch.bedroom
if export is another >1000, turn off switch bedroom, turn on switch.living2 (2000w)
and finally if another > 1000 turn on switch.bedroom

Then of course turn them off in the reverse senario if grid_usage is >0

I’ve found some code in the forums for getting the grid usage out of the inverter and making it positive, but i’m a bit lost how to use it correctly. I tried the GUI with a simple automation but it doesn’t work with the few iterations i’ve tried. Here’s the code for getting the data out of the inverter, and when run in dev tools template, outputs the correct data.

- platform: template
  sensors:
    fronius_grid_export:
      unit_of_measurement: 'kW'
      value_template: >
        {% if states('sensor.fronius_grid_usage') | int > 0 %}
          0
        {% else -%}
          {{ (states('sensor.fronius_grid_usage') | int) | abs }} 
        {% endif %}      

    fronius_grid_use
      unit_of_measurement: 'kW'
      value_template: >
        {% if states('sensor.fronius_grid_usage') | int > 0 %}
          {{ states('sensor.fronius_grid_usage') }}
        {% else -%}
          0
        {% endif %}

I know i’ll need to use if and else satements and read the states of the switches which I can probably get my head around but i’m a bit lost as where to start. Any help and pointers welcome.

Just thought i’d update this as I’ve changed a few things today and got the sensors to work and have cards working on my dashboard. I’ve been trying both the official fronius platform and the HACS fronius_inverter platform. Both seem to work the same so for now i’ll stick with the official version and post up my configuration and automation files to see if anyone can work out why even my simple automation wont work.

configuration.yaml


#sensor:
#  - platform: fronius_inverter
#    ip_address: 192.168.1.16
#    powerflow: true
#    power_units: W
#    units: kWh
#    smartmeter: true
#    scan_interval: 6
    
sensor:
  - platform: fronius
    resource: http://192.168.1.16
    monitored_conditions:
    - sensor_type: inverter
      device: 1
    - sensor_type: meter
      device: 3
    - sensor_type: power_flow
    scan_interval: 2
    
  - platform: template
    sensors:
      fronius_grid_export:
        unit_of_measurement: 'W'
        value_template: >
          {% if states('sensor.power_grid_fronius_power_flow_0_http_192_168_1_16') | int > 0 %}
            0
          {% else -%}
            {{ (states('sensor.power_grid_fronius_power_flow_0_http_192_168_1_16') | int) | abs }} 
          {% endif %}      

      fronius_grid_use:
        unit_of_measurement: 'W'
        value_template: >
          {% if states('sensor.power_grid_fronius_power_flow_0_http_192_168_1_16') | int > 0 %}
            {{ states('sensor.power_grid_fronius_power_flow_0_http_192_168_1_16') }}
          {% else -%}
            0
          {% endif %}
        

edit: removed text, since fixed

Here is my latest simple automation.yaml which doesn’t work.

- id: '1627917436517'
  alias: bedroom heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    id: exporting
    attribute: unit_of_measurement
    above: '1000'
    value_template: '{{state_attr(''sensor.fronius'',''grid_use'')}}'
  condition:
  - condition: numeric_state
    entity_id: sensor.fronius_grid_use
    below: '1'
    attribute: unit_of_measurement
    value_template: '{{state_attr(''sensor.fronius'',''grid_use'')}}'
  action:
  - service: switch.turn_on
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single

I’m a bit lost on this one.

OK, managed to clear old entities and fronius_grid_use is now available and automations.yaml edited to suit.

The “attributes:” and “value_template:” seems unnecessary, here.

Try the following as a starting point.

- id: '1627917436517'
  alias: bedroom heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    id: exporting
    above: 1000
  condition:
  - condition: numeric_state
    entity_id: sensor.fronius_grid_use
    below: 1
  action:
  - service: switch.turn_on
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single

Thanks koying, I had it that way before I added the state_attr but it didn’t work then either.re-edited and still no go. In the log is a weird thing. Three entries all with the same timestamp down to the second. bedroom heater turned off then bedroom heater became unavailable, then bedroom heater turned on.
But the switch doesn’t show as on.

OK,getting somewhere now. I just set the state to 1200w for grid_export (sun is going down so setting in developer tools) and the switch turned on. :slight_smile:
I have to remove the condition of grid_use so I can test the automation by setting grid_export to >1000 in developer tools. Is that condition even needed really? I’ve now set the switch to turn off if grid_export is <1000. Here is my automation.yaml now which seems to be working ok.

- id: '1627917436517'
  alias: bedroom heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    id: exporting
    above: 1000
  action:
  - service: switch.turn_on
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single



- id: '1627979956806'
  alias: bedroom heater off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    below: '1000'
    id: not exportin enough
  condition: []
  action:
  - service: switch.turn_off
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single

Is there a more elegant way of doing this? To get the other heaters to turn on, do I just set conditions in the gui, or would I be better off writing the file manually with if else statements and testing states of other switches?

My logic brain is going to need help with this I think. I’ve gotten the turning on logic “almost” right, but the turning off logic is harder to work out. It’s the logic between having the 1000w heater on if grid_export is between 1000-2000 and switching which device on and off is what I am having trouble wrapping my head around. I’m not entirely sure I can do it in the automation GUI?
Here is my automation file so far. It works fine if I firstly set grid_export to >1000, bedroom heater comes on. Then if I increas it to >2000, living1 heater comes on, then I need to decrease it to below 2000 and again cross the > 2000 threshold for living3 to come on.
It’s setting the conditions with the 1000w heater vs the 2000w heater is the tricky part.

- id: '1627917436517'
  alias: bedroom heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    id: exporting
    above: 1000
  action:
  - service: switch.turn_on
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single
- id: '1627979956806'
  alias: bedroom heater off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    below: '1000'
    id: not exportin enough
  condition: []
  action:
  - service: switch.turn_off
    target:
      device_id: 037cf4f9ac9daa817db6d2a7e499b43f
  mode: single
- id: '1627982634622'
  alias: living1 heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    above: '2000'
  condition:
  - condition: device
    type: is_on
    device_id: 037cf4f9ac9daa817db6d2a7e499b43f
    entity_id: switch.bedroom
    domain: switch
  action:
  - service: switch.turn_on
    target:
      device_id: bca5e632d266af95a3d53a24d7c7b5a8
  mode: single
- id: '1627985120953'
  alias: living2 heater
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    above: '2000'
  condition:
  - condition: device
    type: is_on
    device_id: 037cf4f9ac9daa817db6d2a7e499b43f
    entity_id: switch.bedroom
    domain: switch
  - condition: device
    type: is_on
    device_id: bca5e632d266af95a3d53a24d7c7b5a8
    entity_id: switch.living_1
    domain: switch
  action:
  - service: switch.turn_on
    target:
      device_id: db623dcf0042a1d11ff64baaaf16ae4f
  mode: single
- id: '1627986966020'
  alias: living1 heater off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    below: '1'
  condition:
  - condition: device
    type: is_on
    device_id: 037cf4f9ac9daa817db6d2a7e499b43f
    entity_id: switch.bedroom
    domain: switch
  action:
  - service: switch.turn_off
    target:
      device_id: bca5e632d266af95a3d53a24d7c7b5a8
  mode: single
- id: '1627987108667'
  alias: living2 off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.fronius_grid_export
    below: '1'
  condition: []
  action:
  - service: switch.turn_off
    target:
      device_id: bca5e632d266af95a3d53a24d7c7b5a8
  mode: single

maybe I need to sleep on this. A few too many beers trying to get this up and running! :slight_smile: