Notification with all the "choose" true actions

Hi!

There is any way with automations to do something like this?

trigger -
actions:
  choose
    option 1: if true set a = 1
  choose
    option 1: if true set b = 2
  choose 
    option 1: if true set c = 3
  notify
    message: [a, b, c]

If 1st and 3rd “choose” actions were true and 2nd “choose” action was false, I should receive a message with “1 3”

I hope it is clear what I want to achieve :smiley:

Thanks in advance!

You can concatenate messages.

trigger -
actions:
  choose
    option 1: if true set a = "1"
  choose
    option 1: if true set a = a ~ " 2"
  choose 
    option 1: if true set a = a ~ " 3"
  notify
    data:
      message: "{{ a }}"

You are right, but I don’t know how to make the “set” thing into automation :smiley: I mean… There is no template action where to have only “set variable” as action, then, I don’t know if the variable will keep value between actions, till the last action which will be notify…

Maybe with a “choose” action where I can add a secondary condition which will only have this “set”… I will try now

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: