Help with a rule

hey guys, I need help with a rule.

what im trying to achieve is this.

when sensor.solarnet_power_grid changes then if input_number.tesla_power_load_helper is more than sensor.solarnet_power_photovoltaics then set input_number.tesla_solar_to_grid_helper to 0

but if input_number.tesla_power_load_helper is less than sensor.solarnet_power_photovoltaics then input_number.tesla_solar_to_grid_helper to the state of sensor.solarnet_power_photovoltaics - input_number.tesla_power_load_helper

ive tried with chat gpt but it keeps throwing errors and has different code each time

Don’t. It produces confident gibberish and if you can’t spot this it will only make you more confused. There’s a reason why we don’t allow chat gp answers to help others in this forum.

automation:

trigger:
  - platform: state
    entity_id: sensor.solarnet_power_grid
    to:  # null to triggers on any state change but ignores attribute changes
condition:
  - condition: template
    value_template: "{{ states('input_number.tesla_power_load_helper')|has_value and states('sensor.solarnet_power_photovoltaics')|has_value }}"
action:
  - if:
      - condition: numeric_state
        entity_id: input_number.tesla_power_load_helper
        above: sensor.solarnet_power_photovoltaics
    then:
      - service: number.set_value
        target:
          entity_id: input_number.tesla_solar_to_grid_helper
        data: 
          value: 0
  - if:
      - condition: numeric_state
        entity_id: input_number.tesla_power_load_helper
        below: sensor.solarnet_power_photovoltaics
    then:
      - service: number.set_value
        target:
          entity_id: input_number.tesla_solar_to_grid_helper
        data: 
          value: "{{ states('sensor.solarnet_power_photovoltaics')|float(0) - states('input_number.tesla_power_load_helper')|float(0) }}"

Note that you have not specified an action for when input_number.tesla_power_load_helper equals sensor.solarnet_power_photovoltaics. In this case with the above config, nothing happens.

thanks for your help and advice, it doesn’t seem to be working tho.
sensor.solarnet_power_photovoltaics is currently at 0W as its night
input_number.tesla_power_load_helper is currently at 1,334W

Try this instead:

condition:
  - condition: template
    value_template: "{{ states('input_number.tesla_power_load_helper')|is_number and states('sensor.solarnet_power_photovoltaics')|is_number }}"

has_value is a new addition and may still have some bugs.

thanks tom, were getting close now,

input_number.tesla_solar_to_grid_helper never gets changed tho.

Ugh. Wrong service. Change this:

service: number.set_value

To this (in both places):

service: input_number.set_value

That’s got it, you’re a bloody legend, thanks mate!!

Here’s the working automation.

alias: Tesla Solar to grid card
description: ""
trigger:
  - platform: state
    entity_id: sensor.solarnet_power_grid
    to: null
  - platform: state
    entity_id: sensor.solarnet_power_photovoltaics
    to: null
condition:
  - condition: template
    value_template: >-
      {{ states('input_number.tesla_power_load_helper')|is_number and
      states('sensor.solarnet_power_photovoltaics')|is_number }}
action:
  - if:
      - condition: numeric_state
        entity_id: input_number.tesla_power_load_helper
        above: sensor.solarnet_power_photovoltaics
    then:
      - service: input_number.set_value
        target:
          entity_id: input_number.tesla_solar_to_grid_helper
        data:
          value: 0
  - if:
      - condition: numeric_state
        entity_id: input_number.tesla_power_load_helper
        below: sensor.solarnet_power_photovoltaics
    then:
      - service: input_number.set_value
        target:
          entity_id: input_number.tesla_solar_to_grid_helper
        data:
          value: >-
            {{ states('sensor.solarnet_power_photovoltaics')|float(0) -
            states('input_number.tesla_power_load_helper')|float(0) }}

Just FYI I worked out what I did wrong with |has_value.

Instead of this:

{{ states('input_number.tesla_power_load_helper')|has_value and ...

It should be this:

{{ 'input_number.tesla_power_load_helper'|has_value and ...

Or this:

{{ has_value('input_number.tesla_power_load_helper') and ...