Tracking On and Off Times for Devices

Hello! I am trying to set up a sensor or something of that sort that will show the time a device/entity turned off and on. I have found some help and it worked for a little bit but I am not sure why everything is not working. Basically, I have setup a dashboard for my fish tanks. I want to see what time the device turned off and what time it turned on.

This is what I have in my sensors.yaml

- platform: history_stats
  name: Blackwater Air Pump On
  entity_id: switch.blackwater_air_pump
  state: 'on'
  type: time
  end: "{{ now().replace(hour=0, minute=0, second=0) }}"
  duration:
    hours: 24
- platform: history_stats
  name: Blackwater Air Pump Off
  entity_id: switch.blackwater_air_pump
  state: 'off'
  type: time
  end: "{{ now().replace(hour=0, minute=0, second=0) }}"
  duration:
    hours: 24

This is what I had in my templates.yaml (not sure where this should actually go)

- trigger:
   - platform: state
     entity_id: switch.blackwater_air_pump
     to: 'on'
   - sensor:
     - name: Blackwater Air Pump On
       state: "{{ now().timestamp() | timestamp_custom('%I:%M %p %a') }} "
- trigger:
   - platform: state
     entity_id: switch.blackwater_air_pump
     to: 'off'
   - sensor:
     - name: Blackwater Air Pump Off
       state: "{{ now().timestamp() | timestamp_custom('%I:%M %p %a') }} "

This is the error that I get:

Logger: homeassistant.config
Source: config.py:820
First occurred: 12:20:24 AM (25 occurrences)
Last logged: 12:20:44 AM

Invalid config for [template]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/templates.yaml, line 170).
Invalid config for [template]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/templates.yaml, line 178).
Invalid config for [template]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/templates.yaml, line 187).
Invalid config for [template]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/templates.yaml, line 195).
Invalid config for [script]: expected a dictionary. Got OrderedDict([('default_config', {}), ('system_health', {}), ('frontend', OrderedDict([('themes', OrderedDict([('kibibit', OrderedDict([('background-image', "center / cover no-repeat fixed url('https://thatkookooguy.github.io/https-assets/bg-kibibit-theme.png')"), ('lovelace-background', 'var(--background-image)'), ('primary-color', '#209cee'), ('light-primary-color', '#B6B6C1'), ('primary-background-color', '#212121'), ('secondary-background-color', 'rgba(25, 25, 25, 0.7)'), ('divider-color',.... (See /config/configuration.yaml, line 30).

Full disclosure I am not very good at this coding business. I have come really far but still this stuff gets me lost. Any help appreciated or if there is a less bulky way that would also be great! thanks!

The sensor keys should be at the same level as the trigger keys and they shouldn’t have a hyphen.

- trigger:
    - platform: state
      entity_id: switch.blackwater_air_pump
      to: 'on'
  sensor:
    - name: Blackwater Air Pump On
      state: "{{ now().strftime('%I:%M %p %a') }}"
- trigger:
    - platform: state
      entity_id: switch.blackwater_air_pump
      to: 'off'
  sensor:
    - name: Blackwater Air Pump Off
      state: "{{ now().strftime('%I:%M %p %a') }}"
1 Like