How to use my template in an automation without error?

Hi,
I am new to HA and try to automate my inverter output power due to the new German law Solarspitzengesetz.
This is how my configuration looks like:

template:
  - sensor:
      - name: "fronius_symo_pwr_limit"
        # Solarspitzengesetz: maximum grid feed is 60% of kWp installed
        # i.e. 5220 * 60% = 3120 W
        # inverter max power is 5000 W
        # value to control inverter power is in percent scaled by 100, i.e. 10000 = 100%
        state: >
          {% set max_power = 5000 %}
          {% set scaling = 10000 %}
          {% set installed_Wp = 5220 %}
          {% set max_grid_feed = (installed_Wp * 0.60) %}
          {{ (( [ ((states("sensor.shellypro3_total_active_power") | float(0) )
                 + (states("sensor.fronius_symo_5_0_leistung_ac")  | float(0) )
                 + max_grid_feed), max_power] | min)
              / max_power * scaling) | int
          }}

This code is fine in the template editor and when reloading the yaml.

Now, I want to use new sensor (fronius_symo_pwr_limit) in an automation like this:

- id: '1743520805814'
  alias: Froinuis Symo limit output power
  description: ''
  triggers:
  - trigger: state
    entity_id:
    - sensor.shellypro3_total_active_power
  conditions:
  - condition: numeric_state
    entity_id: sensor.shellypro3_total_active_power
    below: -3000
  actions:
  - action: modbus.write_register
    data:
      hub: mb_symo
      address: 40242
      value: '{{ states(''fronius_symo_pwr_limit'') }}'
  - action: modbus.write_register
    data:
      hub: mb_symo
      address: 40246
      value: 1
  mode: single

When I manual triggering the action, I receive an error expected int @ data['value'][0]. Got None. Writing fixed values (s. second action) works fine.

What have I missed? Thanks for your help!

I am not 100% as I am learning as well but after restarting (w/o the new sensor) put this into the template editor :
‘{{ states(’‘fronius_symo_pwr_limit’‘) }}’

what do you get?

my guess … is that you get a bunch of info … including last updated at the end
I usually do this instead:
‘{{ states.sensor.fronius_symo_pwr_limit.state }}’

try putting that into development → template editor and see what it says. … hopefully it will be the value you want.

That is not an entity id, you are missing the domain. states() requires an entity id.

Perhaps you meant:

value: '{{ states(''sensor.fronius_symo_pwr_limit'') }}'

Do not do this:

It will cause errors. See:
https://www.home-assistant.io/docs/configuration/templating/#states

2 Likes

Thanks, @tom_l and @Patrch for your help. Sure, the domain was missing - thanks for pointing out!

Another question: just loading the yamls is not enough to activate the template? Which ‘depth’ of reboot is necessary for this?

Likely a restart … instead of a quick reload.

in general I do restart when doing yaml as it always works it seems as opposed to the quick reload which does not always work and I have not figured out exactly why and then i think it saves time to restart vs wasting time with the quick reload and the time figuring out it didn’t work

if you hit c on a dashboard and type reload you can see what the quick reloads does I believe.
:upside_down_face:

The first time you add any new integration you need to restart.

After that a reload is sufficient.

1 Like

Another follow up question. I want to have a graph of the sensor state over time to compare it with other sensor data. However, I only get a color-coded bar, no xy-graph. Is the template missing anything to instruct HA to do that?