Global variables that are stored between automations - without using add-ons

There are various solutions and suggestions for storing global viariable in hassio, such as MQTT topics and using add-ons such as: https://github.com/rogro82/hass-variables or as binary sensors such as this one: https://community.home-assistant.io/t/any-suggested-methods-or-best-practices-for-storing-variables/3399.

I deciced after a bit of searching to use the MQTT path, and because I could not find many examples I am posting this for future reference.

The use case was this:

  • Alarm automation triggered by a list of door sensors and PIR, this triggers the alarm and stores the name of the sensor that triggered it

  • Second automation triggers on alarm pending, and plays a warning tone to give me time to disarm the alarm

  • Third automation that triggers when the alarm goes from pending to triggered, plays a loud alarm sound and sends a SMS notifcation which includes the original triggering sensor name.

I have used the MQTT device tracker as the variable storage, as this allows you to store an arbitrary string as the object value. If I was going to store a numeric value, or a binary value, I would have used a MQTT sensor or binary sensor respectively.

The code is as follows -

Define the object in configuration.yaml to store the variable, the MQTT topic is the “variable” name:

# Device tracker masquerading as variable storage
device_tracker:
  - platform: mqtt
    devices:
        sensor_name: 'sensor/name'

Store the variable value when alarm triggered (last 4 lines of this code block), using mqqt.publish to call the MQTT Notify service:

  - alias: 'Trigger when armed_away'
  
    trigger:
      - entity_id:  binary_sensor.front_door, 
                    binary_sensor.west_front_window,
                    binary_sensor.east_front_window,
                    binary_sensor.office_window,
                    binary_sensor.lounge_door,
                    binary_sensor.sunroom_east_door,
                    binary_sensor.back_door,
                    binary_sensor.shed_pir
        platform: state
        from: 'off'
        to: 'on'
    condition:
      - condition: state
        entity_id: alarm_control_panel.alarm
        state: armed_away
    action:
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.alarm
      - service: mqtt.publish
        data_template:
          payload: "{{ trigger.entity_id }}"
          topic: sensor/name
          retain: true

This is the code that runs while the alarm is pending, it does not use the variable, just presented here for interest sake:

  - alias: 'Alarm pending'
  
    trigger:
      - platform: state
        entity_id: alarm_control_panel.alarm
        from: 'armed_away'
        to: 'pending'
      - platform: state
        entity_id: alarm_control_panel.alarm
        from: 'armed_home'
        to: 'pending'
    action:
      - service: media_player.turn_on
        entity_id: media_player.office_speaker
      - service:  media_player.volume_set
        entity_id: media_player.office_speaker
        data:
          volume_level: 0.5
      - service:  media_player.play_media
        data:
          entity_id: media_player.office_speaker
          media_content_id: http://xxx.yyy.net/audio/entry.mp3
          media_content_type: audio/mp3

Finally, the code that sounds the alarm and sends a SMS message, again the last few lines of this code are the relevant bit:

  - alias: 'Send notification when alarm triggered'
  
    trigger:
      - platform: state
        entity_id: alarm_control_panel.alarm
        to: 'triggered'
    action:
      - service:  media_player.volume_set
        entity_id: media_player.office_speaker
        data:
          volume_level: 1
      - service:  media_player.play_media
        data:
          entity_id: media_player.office_speaker
          media_content_id: http://xxx.yyy.net/audio/intruder.mp3
          media_content_type: audio/mp3
      - service_template: notify.alarms
        data_template:
          message: >
            My house alarm triggered - sensor:  {{  states('device_tracker.sensor_name') }}
          target:
            - '+6149999999'

Hopefully this will be of assistance and save someone some time.

1 Like

Thank you! I think this may finally be an elegant solution to my wanting a simple state machine for things more complex combinations of sensor data without the concern of unintended interaction between automation.

1 Like