Sum of four input for activation

I’ve a Shelly 4PM pro device . I need to create a scene that the power sum of the four lines is the condition to activate a switch.
I’ve found the way to do that with " and " but it is not what I need.

thanks for your help

How is the sum of the sensors a condition?

Do you mean to say that the condition is if the sum of the four sensors is above (or below) a threshold value?

yes . you re right

Use a Template Condition.

condition:
  - condition: template
    value_template: >
      {{ states('sensor.sensor_1') | float(0) + states('sensor.sensor_2') | float(0) + 
         states('sensor.sensor_3') | float(0) + states('sensor.sensor_4') | float(0) > 500 }}

Another way to do the same thing:

  - condition: template
    value_template: >
      {{ expand('sensor.sensor_1', 'sensor.sensor_2', 'sensor.sensor_3', 'sensor.sensor_4')
         | map(attribute='state') | map('float',0) | sum > 500 }}

ok,
please could you explain how to add this code ? I’ve to call the condition “template” in my scene? is it possible to change the name from " template " ?
thanks

Have you read the documentation for Scenes?

A scene simply sets entities to a chosen value. It doesn’t support conditions, templates, etc.

Perhaps what you want is an automation.

I’ve done the debug with the code you suggest me and the boolean string changes correctly.

Creating automation in the activation menu I choose template and put the code .

What did you select for the automation’s Trigger?

When a template triggers

it works !. i’ve used the visual editor and copied all the code.
I cut the first two rows of the code.

Now I’ve to add the condition that fires on sum for 20 seconds

What exactly do you mean by “on sum for 20 seconds”?

If you are using it as a Template Trigger then it supports the for option.

trigger:
  - platform: template
    value_template: >
      {{ expand('sensor.sensor_1', 'sensor.sensor_2', 'sensor.sensor_3', 'sensor.sensor_4')
         | map(attribute='state') | map('float',0) | sum > 500 }}
    for:
      seconds: 20

If you are using it as a Template Condition, a Template Condition doesn’t support the for option.

I mean that trigger should fires if the sum > 500 is for 20 sec

In that case you will need to create a Template Trigger, not a Template Condition. My previous post contains an example.

alias: example
trigger:
  - platform: template
    value_template: >
      {{ expand('sensor.sensor_1', 'sensor.sensor_2', 'sensor.sensor_3', 'sensor.sensor_4')
         | map(attribute='state') | map('float',0) | sum > 500 }}
    for:
      seconds: 20
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.shelly
1 Like

I’ve just created a template trigger but with “for: seconds: 20” doesn’t fire.

The addition of this:

    for:
      seconds: 20

means the sum of the four sensor values must exceed 500 for a minimum of 20 consecutive seconds in order for it to trigger the automation. If the sum exceeds 500 for less than 20 seconds, it won’t trigger the automation.

I’m doing the test on bench with fixed load so I know if the sum exceeds .

This is the complete automation code :slight_smile: alias: esubero potenza 40w per 10 secondi :

description: esubero potenza di 40w di 4 linee per 10 secondi
trigger:
  - platform: template
    value_template: >-
      {{ expand('sensor.shellypro4pm_ec62609fd5b0_switch_0_power',
      'sensor.shellypro4pm_ec62609fd5b0_switch_1_power',
      'sensor.shellypro4pm_ec62609fd5b0_switch_2_power',
      'sensor.shellypro4pm_ec62609fd5b0_switch_3_power')
         | map(attribute='state') | map('float',0) | sum > 40 }}
    for: 00.00.10
condition: []
action:
  - device_id: xxxxxxxxxxxxxxxxxxx
    domain: mobile_app
    type: notify
    message: esubero potenza
    title: esubero potenza 40w
mode: single

When posting your automation, please format it. Use the the form editor’s </> icon. For more information, refer to guideline 11 in the FAQ.


I believe the for option’s value should look like this:

    for: '00:00:10'

It’s important to understand how a Template Trigger works. It triggers when the template’s result changes from false to true.

What that means for your application is that, initially, the sum of the four sensors must be below 40 (the template evaluates to false). When the sum of the four sensors increases above 40 (and remains above 40 for at least 10 seconds) the template’s result will be true. It’s the change from false to true that triggers it.

So if the sum of the four sensors was already above 40 when you created the automation, the Template Trigger will not trigger because it’s already true. It’s waiting for a change from false to true.

I’ve wrote again the code. Now it seams to work . :slightly_smiling_face:

If I’d like to send a second notification after 30 sec from the first one ?

I made another automation with different " for " value