Graph of when automation was triggered

Hi @Alec - it looks like your original state is ‘On’ rather than ‘on’.
Have you tried using an upper case O rather than the lower case o in the template sensor that @petro provided.

I have the same problem trying to report river levels. I have the values but am getting the bar graph. I’ve tried putting in the unit_of_measurement. Can someone please verify whether this goes into the lovelace file, or if it has to be an attribute of the entity being displayed? And can you verify the name is unit_of_measurement or unit_of_measure, or some other version of that phrase?

does your state include the units or are the units separate?

Thanks for the suggestion @chairstacker, I’ve tried that but no impact:

screenshot

Current Lovelace card config:

type: history-graph
title: My Automation
entities:
  - sensor.my_automation
hours_to_show: 168
refresh_interval: 0
unit_of_measurement: kwh

Sensor config:

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

Wouldn’t your value turn the result into a string due to the quotation marks around it.
Have you tried it without or with just single quotes?

That’s not working because your template has a capital O for on.

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

Also, you will need to purge your database because now it thinks it’s a string.

@chairstacker, it work because 1 or 0 are integers. The parser will attempt to convert to a number first.

If he adds a unit of measure, shuts off HA, clears database, and restarts HA, then it should show up as a graph. Assuming that automation.my_automation is the name of his automation.

Thanks @petro I have switched back to 'on'.

The unit_of_measurement in the Lovelace card config is set to kwh.

shuts off HA, clears database, and restarts HA

I’m using Hass.io, not sure how to do this? As mentioned above I’ve tried this which is all I could find on the topic.

I mean, if you don’t care about your history, just delete the file. That’s the easiest way. Sometimes purging the database doesn’t work with the service. And to be honest, you should probably delete the whole thing anyway because I’m not entirely sure if the item will be removed if you purge.

Thanks, I have deleted all home-assistant_v2.db files from /config and restarted which has cleared the history. Hopefully I’ll have a graph in a day or two, will report back!

screenshot

well, that’s already a problem. unknown will not change it to a numerical result. It should be showing a graph right now at zero.

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.