Graph of when automation was triggered

can you post your automation and your template sensor? Possibly your entire config?

Also, I just realized you are putting the unit_of_measure on the wrong object. It should be attached to your sensor, not in the configuration for history-graph.

@petro thank you, I now have a line graph!

- platform: template
  sensors:
    my_automation:
      unit_of_measurement: 'kwh'
      value_template: "{{ 1 if is_state('automation.my_automation', 'on') else 0 }}"

screenshot

Looks like 1 is set each time HA restarts, when all automations get set to on which I assume is normal behaviour. These are the only values that appear on the graph.

Thing is, I don’t want to record on the graph each time the automation is switched on I want to record each time the automation is triggered.

1 Like

Hmm, if you want to record triggers, you may need to re-think this. Do you want this to record when it runs actions or triggers. Because conditions can stop it from running actions but the trigger will still occur.

I’d like to record when it runs the actions ideally.

One idea is to use another sensor that gets set to 1 and then 0 in the script. Then that sensor would only change state when the script runs.

@MatthewFlamm thanks, that sounds like exactly what I’m after.

This sounds simple, but I don’t see a way to set the value of a sensor via a script, even using a Template Binary Sensor.

Any ideas on how I can change the value of an entity from my script (I could set it to 1 at the beginning of the script, 0 at the end) which I can then make a Template Binary Sensor out of to achieve this?

I’ve used input_number, or similar. It will do what you want, but the downside is that it will create a way to manually change the number too. Even if it isnt in a view, you’ll be able to find it in unused entities.

@MatthewFlamm thank you this is now working as expected with input_number.

I’ve summarised the setup below in case it’s helpful to anyone. To record each time a “Going to Bed” automation is triggered over a week for example:

Input Number

First an input_number is needed so there’s something to switch on/off.

input_number:
  going_to_bed:
    name: Going to Bed
    min: 0
    max: 1

Template Sensor

Then a Template Sensor is made based on the input_number. This sensor is what the graph uses (note the kwh).

- platform: template
  sensors:
    bedtimes:
      unit_of_measurement: 'kwh'
      value_template: "{{ 1 if is_state('input_number.going_to_bed', '1.0') else 0 }}"

Script

This could be done in an automation, but in my case an automation calls this script. The script sets the input_number to 1 at the beginning, and back to 0 at the end.

bedtime_shutdown:
  alias: Bedtime Shutdown
  sequence:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.going_to_bed
        value: 1
    # etc.
    - service: input_number.set_value
      data_template:
        entity_id: input_number.going_to_bed
        value: 0

Lovelace History Graph Card

Finally the Lovelace card is shown in the UI.

type: history-graph
title: Bedtimes
hours_to_show: 168
refresh_interval: 3600
entities:
  - sensor.bedtimes

Due to the unit_of_measurement in the Template Sensor this shows as a line graph:

screenshot
(only 2 days of data in above graph)

3 Likes

Nice!

Coming back to this, I wonder if it would have been simpler to use a counter depending on the use case. Although you don’t get this behavior, only cumulative number of times.