Trying to configure motion history

Hello - I’m trying to configure motion and sensor history from @oakbrad site here

I’ve troubleshot this a lot, but I can’t wrap my head around what I’m doing wrong. It seems to not like the sensor name or where I’m inserting it, but I can’t figure it out.

I don’t have a binary_sensor.yaml configured and when I tried to create the file, I couldn’t get anything to work, so I just put those bits in configuration.yaml instead. I think I placed it in correctly.

  - platform: template
        friendly_name: 'Main Hallway Front Motion'
        device_class: motion
        value_template: >-
          {%- if is_state("sensor.main_hallway_lights_front_motion_sensor", "on")
          {%- else -%}
          {%- endif %}

This is also in configuration.yaml

    value: 'Unknown'
    restore: true
      icon: mdi:map-marker
      name: "Last Motion"


# Update Last Motion variable
- alias: "Update Last Motion"
    - platform: state
      entity_id: sensor.basement_hallway_lights_motion_sensor,sensor.main_hallway_lights_front_motion_sensor,sensor.main_hallway_lights_rear_motion_sensor,sensor.main_stairs_motion_sensor,sensor.master_bathroom_motion_sensor,sensor.master_bedroom_night_motion_lightsensor,sensor.master_closet_motion_sensor
      to: 'on'
    - service: variable.set_variable
        variable: last_motion
        attributes_template: >
              "history_1": "{{ variable.state }}",
              "history_2": "{{ variable.attributes.history_1 }}",
              "history_3": "{{ variable.attributes.history_2 }}"
        value: "{{ trigger.to_state.attributes.friendly_name }}"

The error I receive is here:


Have you got ‘binary_sensor:’ before the template?

1 Like

Holy cow… That was all it was… :blush:

Thank you - wow… :joy:

Even after making that fix, I still don’t get this card with history.

Here is my motion sensor as binary_sensor and customize to show

  - platform: mqtt
    state_topic: "brokername/motionsensorname"
    name: "Office Motion 2"
    payload_on: 'Motion_Detected'
    payload_off: 'No_motion'
    value_template: '{{ value_json.motion }}'
    device_class: motion

      friendly_name: Office Motion 2
      show_last_changed: true
      icon: mdi:walk


That’s cool, but it doesn’t achieve the look and function I was going after. But thank you for sharing.

What does your card look like?

You have the variable custom_component installed, correct? For reference here is what I have, I am not sure if the variable component has been updated or changed since I first set this up:

Hi! Haha! So totally my fault. When I rebuilt and migrated to this install, I forgot to copy or reinstall the hass-variables. Doh!

So that’s sorted and yes, I get the card, but it’s not showing state history.

1 Like

Hmm that is strange, it looks like you are getting the history attributes just not the graph? So the automation and sensors are working, just the variable component isn’t getting the state history.

Do you get any errors in the logs? Can you get a state history graph on any other variable entity?

You might want to ask the developer of the variable component over in this thread:

No error in logs and I too thought it was strange. It’s clearly capturing the history, but not graphing it. I don’t know how to graph anything else. Still learning. Right now I can’t even get to the web interface, but the instances are started.

Any way, likely something I’m doing wrong. I’ll hop over to that thread and get some help on correcting it.

Thanks @oakbrad

@oakbrad @Sunonline @keithh666

Edit: Problem solved and it solved for all of my graph issues throughout.

I was seeing sql errors like below in the homeassistant logs:
sqlite3.DatabaseError: database disk image is malformed

They included messages about recorder and other things. My Logbook and History has not been working either.

After searching, it appears that this is caused by a corrupt home-assistant_v2.db file. The recomendation was to delete it and let HASS recreate one.

Since doing so, ALL graphs work - including state history and my Logbook and History are working as well!

Thank you.


with the new 115 update this stopped working for me. I am getting
"homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: ‘variable’ is undefined"

I checked if “variable” domain is created and it’s indeed there. I am sure this has something to do with new templating. Anyone else getting this or managed to get this fixed?

There is a fix posted here:
you have to add state.variable.entity-id now

- id: update_last_open_door   
  alias: "Update Last Open Door"
    - platform: state
      entity_id: binary_sensor.front_door, binary_sensor.garage_door, binary_sensor.patio_door
      to: 'on'
  mode: queued
    - service: variable.set_variable
        variable: last_open_door           
        attributes_template: >
          "history_1": "{{ states.variable.last_open_door.state }}",
          "history_2":"{{ states.variable.last_open_door.attributes.history_1}}",
          "history_3":"{{ states.variable.last_open_door.attributes.history_2}}",
          "history_4":"{{ states.variable.last_open_door.attributes.history_3}}"
        value: "{{ trigger.to_state.attributes.friendly_name }}"  
1 Like

Hello - wow! Reviving a 3-year-old thread here :slight_smile: But it still seems relevant so I’ll stay here.

OK - so looking for some help to get this working again. I’ve applied what was mentioned here - but it’s still not kicking in for me.

Here is what is in my configuration.yaml

    value: Not set
    restore: true
      icon: mdi:map-marker
      friendly_name: Last Motion

Here is what is in my automations.yaml

# Update Last Motion variable
- alias: "Update Last Motion"
  id: "Update Last Motion"
    platform: state
      - sensor.front_door_last_motion
      - sensor.keene_front_door_last_motion
      - binary_sensor.basement_hallway_lights_sensor_motion
      - binary_sensor.front_door_motion
      - binary_sensor.main_hallway_lights_front_sensor_motion
      - binary_sensor.main_hallway_lights_rear_sensor_motion
    to: "on"
    service: variable.set_variable
      variable: last_motion
      attributes_template: >
      #    data_template:
      value: >
        {{ trigger.to_state.attributes.friendly_name|replace('motion sensor','') }}:
        {{as_timestamp(states.variable.last_motion.last_changed)| timestamp_custom('%X') }}

All I seem to get is the following:

It seems to be the only sensor it’s picking up and I’m not sure why.

Any ideas what is wrong and where?

Sorry to resurrect this again, but looks like it stopped working again on the new 2020.12.0 release. Getting the
template value should be a string for dictionary value @ data['attributes_template']

I too like this automation, but do not have it working either. So, I’m searching for a fix. Do you have any further updates?