Notification with all the "choose" true actions

Seems that the variables does not keep the value between actions :frowning:

I think it can be done by storing every value to an input_text, but… still, there is any other solution? An easier one?

Yeah use an input text.

Damn :smiley:
Automations should have a template as action and keep variables between actions :frowning:

Or declare a variable in your script using a jinja2 template:

variables:
  msg: |
    {% if option1 == true %}1{% endif %}{% if option2 == true %} 2{% endif %}{% if option3 == true %} 3{% endif %}

Oh wait… With this aproach, I don’t even need variables…
I can use something similar of what you told me.
In the last action which is the notifying action, I can write that if/endif thing…knowing the result of all the “choose” actions, right?

Edit: I tested, it is not working :frowning:

alias: test
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{true}}'
        sequence: []
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: '{{false}}'
        sequence: []
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: '{{true}}'
        sequence: []
    default: []
  - service: persistent_notification.create
    data:
      message: >-
        {% if option1 == true %}1{% endif %}{% if option2 == true %} 2{% endif
        %}{%     if option3 == true %} 3{% endif %}
mode: single

The result, should be “1 3” in a persistent notification… instead, it is empty :expressionless:

Well, you should of course replace the option == true clauses with whatever condition you have. If I just use true in the template helper, the string comes out so it should be doable. There’s no need for, or relation to, a choose with this template.

I thought that option1, 2, 3… it is a global “variable”… Because home assistant does know if all the “choose” actions are true or false and thinking of this, I thought that maybe it is stored somewhere… :frowning: I think I will stick to the helper :smiley:

I didn’t try, but I’m kindof surprised that if you declare a variable in a script, that setting it in a choose won’t hold. Kind of defeats the purpose a bit.

You could possibly make a nicer formatted multiline jinja template as well, if that is your concern. The input helper will work if you have only one “single” automation using it, but it feels like overkill. But hey, you’ll have a history of messages :smile:

With the helper, yes, for only one automation, of course. And it will work like tom_l said, concatening the messages… And after the notification, the helper will be empty again, waiting to be filled :smiley:

As usability of variables with “choose”, i’ve created a “choose” condition for the primary purpose, and the second condition only to declare the variable… but yeah… the bad part is that the variables are not keepling the value between actions…

I’ve done like this:

choose:
  - conditions:
      - condition: template
        value_template: '{% if states("sensor.humidity") > 10 %}true{% endif %}'
      - condition: template
        value_template: '{% set variable = "ok" %}{% if variable = "ok" %}true{% endif %}'
    sequence: []
default: []

If first condition will be true, the second will be automatically true and set the variable, but as I thought already (but I tested as well), the variable does not keep between actions :frowning:

I will do it with input_text, and thats it :smiley:

There’s a difference between script variables and Jinja2 variables and their scope. A script variable’s scope can be global for the script whereas a Jinja variable’s scope is always limited to the YAML option in which it’s defined.

The Jinja2 variable named option1 is referenced in the message option but it’s undefined; it’s not defined anywhere in the message option. In addition, if anyone is assuming it is somehow related to the choose statement, it’s not.


Instead of posting your requirements in pseudo-code, describe the application you wish to construct.

I almost finished, but I only need one more information. How the hell I can store a “new line” in the input_text helper? :smiley: I mean… It is only storing strings, no new line? I tried with “\n” but nothing…

I think that input_select could help me… :frowning:

Use two blank lines.

Can you show me an example? I tried but it is not working

example1:
  sequence:
  - variables:
      a: "{{ iif(true, 'bat', 'none') }}"
      b: "{{ iif(true, 'cat', 'none') }}"
      c: "{{ iif(false, 'rat', 'none') }}"
  - service: notify.persistent_notification
    data:
      message: "{{ a, b, c }}"

Screenshot from 2022-05-04 10-46-32

If you explain exactly what your application is (notably the output you expect because your first post doesn’t mention the need to include a new line but your most recent post does) then I can help you achieve your goal.

This is how I achieve what I wanted, but still, if you have any other ideas, all are welcome. Thanks

alias: New Script
sequence:
  - service: input_select.set_options
    data:
      options: '{{ "For the following devices:" }}'
    target:
      entity_id: input_select.incercam
  - choose:
      - conditions:
          - condition: template
            value_template: '{{true}}'
        sequence:
          - service: input_select.set_options
            data:
              options: '{{ states("input_select.incercam") ~ "\naaa" }}'
            target:
              entity_id: input_select.incercam
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: '{{false}}'
        sequence:
          - service: input_select.set_options
            data:
              options: '{{ states("input_select.incercam") ~ "\nbbb" }}'
            target:
              entity_id: input_select.incercam
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: '{{true}}'
        sequence:
          - service: input_select.set_options
            data:
              options: '{{ states("input_select.incercam") ~ "\nccc" }}'
            target:
              entity_id: input_select.incercam
    default: []
  - service: persistent_notification.create
    data:
      message: '{{ states("input_select.incercam") }}'

The notification result, is:

For the following devices:
aaa
ccc

I am trying to make an automation with a notification for the sensors which could have a malfunction … and I use “choose”, in order to know which sensor have a problem, then I set the information into the input_select, and… at the end, I will have the notification

If that’s your goal then you’ve chosen a very peculiar way of achieving it. There may be a simpler solution, implemented entirely with a template, but it depends on the details of your sensors. What is the criteria you are using to determine if a sensor has a problem?


For example, the following template lists the names of all sensors whose state is either unavailable or unknown.

{{ states.sensor | selectattr('state', 'in', ['unavailable', 'unknown'])
  | map(attribute='name') | list | join(', ') }}

I use this, to check if the sensor is not working


{% set time = (as_timestamp(now()) - as_timestamp(states.sensor.broadlink_bedroom_humidity.last_changed)) | int %} {% if time > 14400 %}true{% endif %}

If in 4 hours the humidity won’t change, I am more than 95% that it is not working :smiley:

Here’s a template that outputs a list of all humidity sensors in your house which haven’t had a state change in the last 4 hours.

{{ states.sensor
    | selectattr('attributes.device_class', 'eq', 'humidity')
    | selectattr('last_changed', 'lt', now() - timedelta(hours=4))
    | list
}}

Or if you don’t want to use device_class to find these and you want to use a fixed list then can try this:

{% set sensors = [
  "sensor.broadlink_bedroom_humidity",
  ...(list all humidity sensors)...
] %}
{{ states.sensor
    | selectattr('entity_id', 'in', sensors)
    | selectattr('last_changed', 'lt', now() - timedelta(hours=4))
    | list
}}

Either way there’s really no reason to use any chooses here. Just collect all the broken sensors with a template and notify about them.

Last August, I helped someone else with an almost identical requirement. They wanted to detect “stale sensors” (any sensor whose value hasn’t been updated in the last hour or 4 hours or whatever time period you prefer).

It demonstrates the principal with a few template examples (all similar to what CentralCommand posted). It applies the technique in an automation that runs every 4 hours and checks if any sensors failed to update during the past 4 hours. If it finds stale sensors, ot reports their entity_id via a notification.

Basically, it does most everything you require, without the complexity of the choose you intended to employ.

1 Like

Thank you. I will try to adapt the code to my requirements, because I don’t use the default “last_change”, I made some variables where I store the last change, in order to keep the data after HA restart.

I made it, Thank you again!