Variables for automations

I have several energy monitoring plugs that are used to monitor particular energy guzzling appliances (washing machines, dishwashers, kettles etc).

I have automations in place for the dishwasher for start and end of cycles, based on power through the plug.

The end automation calculates the cost of the cycle based on a dishwasher_start_energy variable, dishwasher_stop_energy variable and the electricy_tariff value from my meter.

It writes a row to a spreadsheet, using IFTTT, with the event “dishwasher_ended”, the start and stop values and the cost value.

It seems to me that there should be a way of using variables so that all plugs could use the same automation (add every one to trigger) but change the actions to reflect the plug that triggered and thus read, say, a washingmachine_start/stop_energy value for the calculation and send a “washingmachine_ended” event to IFTTT.

I guess this would involve calling a script but not sure how the automation could recognise in the action which plug triggered and how to send and use that info in a script.

2 Likes

Here is a practical example of automation using variables and entity which triggered the automation…
This should help you and is answering some of your questions:

- alias: record_change_date_time
  initial_state: true
  trigger:
    - platform: state
      entity_id: 
        - climate.aquatemp_poolstage
        - climate.airco_bureau
        - climate.airco_chambre_ma
        - climate.airco_grenier
        - climate.airco_chambre_parents
        - climate.airco_chambre_thomas
        - climate.veranda_c
        - sensor.status_alarm_courcelles
      to:
  variables:
    entity: >-
            {% set string=trigger.entity_id %}
            {% set string=string.split('.') %}
            {% set result="input_datetime."+string[0]+"_"+string[1]+"_changed" %}
            {{ result }}
  action:
    - choose:
        - conditions:
            - "{{trigger.from_state.state != 'unavailable'}}"
            - "{{trigger.to_state.state != 'unavailable'}}"
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: "{{ entity }}"
              data:
                date: '{{ as_timestamp(now()) | timestamp_custom("%Y-%m-%d") }}'
                time: '{{ as_timestamp(now()) | timestamp_custom("%H:%M:%S") }}'
            - service: notify.message_ha
              data_template:
                message: >-
                    {{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DC-055.1 Date modified for {{ entity }}: state changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
      default:
        - service: notify.message_ha
          data_template:
            message: >-
                {{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DC-055.1 Date NOT modified for {{ entity }}: state changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
4 Likes

Here is another example where the variables are at the level of the triggers:

- alias: status_alarme_sms_Courcelles
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.status_alarm_courcelles
      to: "armed"
      variables:
        message: "Alarme Courcelles activée (ON)"
    - platform: state
      entity_id: sensor.status_alarm_courcelles
      to: "disarmed"
      variables:
        message: "Alarme Courcelles désactivée (OFF)"
    - platform: state
      entity_id: sensor.status_alarm_courcelles
      to: "partial"
      variables:
        message: "Alarme Courcelles activée (PARTIAL)"
    - platform: state
      entity_id: sensor.status_alarm_courcelles
      to: "error"
      variables:
        message: "Erreur Lecture Status Alarme Courcelles (ERROR)"
  action:
    - service: notify.message_ha
      data_template:
        message: '{{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** AC-030.1 Sending SMS Alarme Courcelles: {{message}}'
4 Likes

You can find all the trigger variables in the following: