Change Variable inside an IF Statement with Script or Automation

Hi,
I need to change a variable based on IF statement inside a Script or Automation and at the end do something.
I know that variable is used only inside IF, bu I need to setup variable based on if. It is possible? Here is an example that does not work:

alias: Verifica Finestre Aperte
sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/Tasmota-CortesiaIngresso/MP3TRACK
      payload: "1"
    enabled: false
    alias: "MQTT: Suona Sirena"
  - variables:
      devo_suonare: "{{false}}"
      cosa_suonare: Nulla
  - if:
      - condition: state
        entity_id: binary_sensor.xiaomi_contact_abbaino_contact
        state: "on"
    then:
      - variables:
          devo_suonare: "{{true}}"
          cosa_suonare: Abbaino Mansarda
  - service: notify.pushover
    data:
      title: Test
      message: Devo Suonare, {{cosa_suonare}}
mode: single

use all templates instead, no reason to even use if blocks.

alias: Verifica Finestre Aperte
sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/Tasmota-CortesiaIngresso/MP3TRACK
      payload: "1"
    enabled: false
    alias: "MQTT: Suona Sirena"
  - service: notify.pushover
    data:
      title: Test
      message: >
        {% if is_state('binary_sensor.xiaomi_contact_abbaino_contact', 'on') %}
           Devo Suonare, Abbaino Mansarda
        {% else %}
           Devo Suonare, Nulla
        {% endif %}
1 Like

HI @petro ,
thanks for suggestion, I am new on HA.
I can use template, but I need to do a sequence of IF to check all sensor I need to check and send notification containing all open window, example:

- service: notify.pushover
    data:
      title: Test
      message: >
        {% if is_state('binary_sensor.xiaomi_contact_abbaino_contact', 'on') %}
           Devo Suonare, Abbaino Mansarda
        {% else %}
           Devo Suonare, Nulla
        {% endif %}
        {% if is_state('binary_sensor.xiaomi_contact_cucina_contact', 'on') %}
           Devo Suonare, Abbaino Mansarda, Cucina
        {% else %}
           Devo Suonare, Nulla
        {% endif %}

So in this case I have even to concatenate variable to add only open contact to notification. Even better will be to iterate in a group and add sensor name to notification list based on its status.
In addition the code:

- service: mqtt.publish
    data:
      topic: cmnd/Tasmota-CortesiaIngresso/MP3TRACK
      payload: "1"
    enabled: false
    alias: "MQTT: Suona Sirena"

must be executed only it one window is open and so even notification will be sent out.
It is possible?

Marco

Hi,
more simple than I think before. Using group I can check if a window is open. Here is the code:

alias: Verifica Finestre Aperte
sequence:
  - if:
      - condition: state
        entity_id: binary_sensor.group_stato_porte_finestre
        state: "on"
    then:
      - service: notify.pushover
        data:
          title: Porte o Finestre Aperte
          message: >-
            Finestre/Porte Aperte:    {%- for entity_id in
            states.binary_sensor.group_stato_porte_finestre.attributes.entity_id
            -%}       {% set parts = entity_id.split('.') -%}       {%- if
            states(entity_id) == 'on' %}       {%- if loop.first %} {% elif
            loop.last %}, {% else %}, {% endif -%}{{
            states[parts[0]][parts[1]].name }}{% endif -%}   {%- endfor %}
      - service: mqtt.publish
        data:
          topic: cmnd/Tasmota-CortesiaIngresso/MP3TRACK
          payload: "1"
        enabled: true
        alias: "MQTT: Suona Sirena"
    alias: Verifica se porte e finestre aperte
mode: single