Automation to run every 5 minutes

Hi, I’m not getting any notifications from this, while for sure the valves are all opening less than 99%. Can anyone help explain why, please? The only potential issue I see is the device names are strings of characters instead if device.something.something.

alias: test close
description: ""
triggers:
  - trigger: time_pattern
    minutes: /5
conditions:
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ (state_attr('sensor.device2', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device8', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device4', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device6', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device3', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device5', 'valve_position')|int) < 99 }}"
      - condition: template
        value_template: "{{ (state_attr('sensor.device9', 'valve_position')|int) < 99 }}"
actions:
  - action: notify.mobile_app_coops_phone
    metadata: {}
    data:
      message: CLOSE
mode: single

What does the trace say?

1 Like

I would put each of those conditions in the developer tools template section and see if any were not returning true as expected. I also don’t think you need the AND for the conditions but it also should work the same.

1 Like

Unless you need it to run every five minutes and send you repeat notifications, I’d suggest creating a template trigger with a state like this:

{% set el = ['sensor.device2',
             'sensor.device8',
             'sensor.device4',
             'sensor.device6',
             'sensor.device3',
             'sensor.device5',
             'sensor.device9'] %}
{{ el|map('state_attr','valve_position')|map('float',0)|max < 99 }}

Assuming your entity IDs and the attribute name are correct, that should send a notification each time the template goes from false (all at 99 or above) to true (any valve below 99).

The default value in the float map will also force a trigger if any device isn’t working.

1 Like

Thanks, when I Run action, I get a notification to my phone - so at least the action is working.

I found that in the automation editor UI, clicking the debug icon resulted in

ValueError: Template error: int got invalid input 'None' when rendering template '{{ (state_attr('sensor.device8', 'valve_position')|int) < 99 }}' but no default was specified

So the conditions are obviously where my issue lies. Honestly, I have adapted the conditions without really understanding the relevance.
My automation is intended to check when all radiator valves are open to the same degree (between 0% and 100%). Ultimately I want to activate a switch to close a zone valve when all radiator valves are closed (at 0%), but for now I am trying to generate a notification (based on a test that I know will be met, namely all valves at 99% or less).

Can you just make it

{{ (state_attr('sensor.device8', 'valve_position')|int(0)) < 99 }}

That is just giving it a default value of 0 if it doesn’t have a good value. If I understand it correctly.

What is sensor.device8? Can you post a screenshot from the Developer Tools > States screen on a computer showing state and attributes columns?

1 Like

Also why are you complicating things with template conditions when you can use numeric state conditions?

Thanks for pointing me in the right direction. Changing all those pesky template conditions to plain old numeric state conditions seems to have done the trick. Thank you!

Updated code in case anyone wants to see it (doubt).

alias: test close
description: ""
triggers:
  - trigger: time_pattern
    minutes: /5
conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.device2_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device8_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device4_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device6_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device3_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device5_valve_position
        attribute: value
        below: 99
      - condition: numeric_state
        entity_id: sensor.device9_valve_position
        attribute: value
        below: 99
actions:
  - action: notify.mobile_app_coops_phone
    metadata: {}
    data:
      message: CLOSE
mode: single

The screenshot you posted shows that the sensors don’t have a valve_position attribute. That explains why your template failed.

According to the screenshot, “valve_position” is text within the entity’s entity_id.

sensor.device8_valve_position

This would have worked.

{{ states('sensor.device8_valve_position') | int(0) < 99 }}
1 Like

I’m very confused.
First it was an attribute called valve_position now you post entities called valve_position.

Anyways… remember there can be issues with integration failing or device failing. What would that do?
Could it break something, do you need to make double checks to make sure things something does not happen?

And also… are you sure this needs to run every five minutes?
Wouldn’t it be better to trigger when all is at 100?

Yeah, that’s why I asked for the screenshot after the template error was posted. Just didn’t make sense.

OP: you can remove the attribute lines — the number you want us in both each sensor’s state and the value attribute.

The alternative template trigger:

triggers:
  - trigger: template
    value_template: >
        {% set el = ['sensor.device2_valve_position',
                     'sensor.device8_valve_position',
                     'sensor.device4_valve_position',
                     'sensor.device6_valve_position',
                     'sensor.device3_valve_position',
                     'sensor.device5_valve_position',
                     'sensor.device9_valve_position'] %}
        {{ el|map('states')|map('float',0)|max < 99 }}

…and lose the conditions.

2 Likes

My automation is intended to check when all radiator valves are open to the same degree (between 0% and 100%).

Hm, do you mean this, that they should be equally much open? That has not been part of the suggestions, I think.

The intent is to activate a switch if any of the valves is open (if the sensor value is > 0%) and close the switch if all of the valves are closed (value is = 0%).

What Troon posted will do that too, without nagging every five minutes “are we there yet”

Note that “activate” and “close” mean the same thing with regards to a switch. The solution below will turn the switch on if any valve is open.

Set up a Template Binary Sensor helper (see below) called “Valve open” with state:

{% set el = ['sensor.device2_valve_position',
             'sensor.device8_valve_position',
             'sensor.device4_valve_position',
             'sensor.device6_valve_position',
             'sensor.device3_valve_position',
             'sensor.device5_valve_position',
             'sensor.device9_valve_position'] %}
{{ el|map('states')|map('int',0)|max > 0 }}

This reports off if all the valves are at 0, and on in any other situation.

The default 0 value in the int filter means that an offline valve will be considered shut. Change the 0 to a positive number if you want an offline value considered open.

Then your automation is simply:

triggers:
  - trigger: state
    entity_id: binary_sensor.valve_open
actions:
  - action: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.YOUR_SWITCH_ID

…and will trigger immediately when the answer to the question “are all the valves shut?” changes.

1 Like

Thanks Troon, how do I set up a Template Binary Sensor helper? I looked at Helpers under Devices and services, but I don’t see an edit in yaml option there, so probably the wrong place.

Devices and services > Helpers > Create helper > Template > Binary sensor. That should get you to the blank starting point of my screenshot above.

No YAML involved: the YAML I posted was for the automation. The state template, if that’s what you mean, is a Jinja template and is not YAML.

1 Like