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
sensors:
main_hallway_lights_front_motion:
friendly_name: 'Main Hallway Front Motion'
device_class: motion
value_template: >-
{%- if is_state("sensor.main_hallway_lights_front_motion_sensor", "on")
-%}
True
{%- else -%}
False
{%- endif %}
This is also in configuration.yaml
variable:
last_motion:
value: 'Unknown'
restore: true
attributes:
icon: mdi:map-marker
name: "Last Motion"
automations.yaml
# Update Last Motion variable
- alias: "Update Last Motion"
trigger:
- 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'
action:
- service: variable.set_variable
data:
variable: last_motion
attributes_template: >
{
"history_1": "{{ variable.state }}",
"history_2": "{{ variable.attributes.history_1 }}",
"history_3": "{{ variable.attributes.history_2 }}"
}
data_template:
value: "{{ trigger.to_state.attributes.friendly_name }}"
The error I receive is here: https://hastebin.com/yetafajoho.sql
Thanks!
keithh666
(Keith Hull)
February 6, 2018, 1:32am
2
Have you got ‘binary_sensor:’ before the template?
1 Like
Holy cow… That was all it was…
Thank you - wow…
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
binary_sensor:
- 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
homeassistant:
customize:
binary_sensor.sn1_pir:
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.
oakbrad
(Oakbrad)
February 6, 2018, 8:40pm
7
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:
import asyncio
import logging
import json
import voluptuous as vol
from homeassistant.const import (CONF_NAME, ATTR_ICON)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import template
from homeassistant.exceptions import TemplateError
from homeassistant.loader import bind_hass
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import async_get_last_state
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'variable'
ENTITY_ID_FORMAT = DOMAIN + '.{}'
This file has been truncated. show original
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
oakbrad
(Oakbrad)
February 7, 2018, 7:19pm
9
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:
Because I found myself creating more and more workarounds ( using input_booleans and sliders ) to get some basic things done through automations ( e.g. counters and saving/restoring state ) I wrote a small custom_component for variables.
It allows declaring variables (state) in the configuration and setting/updating variables through service calls.
Source can be found on github:
The readme on github contains all the information to get you set-up and examples for how to use it in automatio…
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.
2 Likes
skynet01
(Alex)
September 18, 2020, 6:34pm
12
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?
skynet01
(Alex)
September 21, 2020, 5:13am
13
There is a fix posted here: https://github.com/rogro82/hass-variables/issues/44
you have to add state.variable.entity-id now
- id: update_last_open_door
alias: "Update Last Open Door"
trigger:
- platform: state
entity_id: binary_sensor.front_door, binary_sensor.garage_door, binary_sensor.patio_door
to: 'on'
mode: queued
action:
- service: variable.set_variable
data:
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}}"
}
data_template:
value: "{{ trigger.to_state.attributes.friendly_name }}"
1 Like
Hello - wow! Reviving a 3-year-old thread here 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
variable:
last_motion:
value: Not set
restore: true
attributes:
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"
trigger:
platform: state
entity_id:
- 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"
action:
service: variable.set_variable
data:
variable: last_motion
attributes_template: >
{
"history_1":"{{states('variable.last_motion')}}",
"history_2":"{{state_attr('variable.last_motion','history_1')}}",
"history_3":"{{state_attr('variable.last_motion','history_2')}}",
"history_4":"{{state_attr('variable.last_motion','history_3')}}",
"history_5":"{{state_attr('variable.last_motion','history_4')}}",
"history_6":"{{state_attr('variable.last_motion','history_5')}}",
"history_7":"{{state_attr('variable.last_motion','history_6')}}",
"history_8":"{{state_attr('variable.last_motion','history_7')}}",
"history_9":"{{state_attr('variable.last_motion','history_8')}}",
"history_10":"{{state_attr('variable.last_motion','history_9')}}"
}
# 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?
skynet01
(Alex)
December 14, 2020, 6:35pm
15
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']
error
bryanbr
(Bryan Bredehoeft)
January 10, 2021, 4:28pm
16
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?