I’d like to create a custom sensor that keeps track of what the last change in the system was. I’m working on my “floorplan” display and I’d like to have an information bar at the bottom that shows what the last change was. For example, if I turn off my kitchen lights at 9:40pm, I’d like the sensor’s state to be “kitchen lights turned off at 9:40pm”. If I then turn off the bedroom lights 5 minutes later, I’d like it to be “bedroom lights turned off at 9:45pm”.
In essence, you would create an automation that monitors state changes on the devices you are interested in. This automation would write the desired text to a system text file.
Then you can create a command line sensor that reads that information from the text file.
it could look something like this:
- alias: test
trigger:
platform: state
entity_id: device1, device2, device3
action:
- service: shell_command.saveState
data_template:
text: 'echo {{trigger.entity_id}} changed to {{trigger.to_state}} at {{now().strftime("%X")"}}'
@treno I tried doing what you suggested, but I’m failing miserably. I’ve been working on this for the better part of two hours and am stumped Mind taking a look at my config? I’m sure I’m just doing something small incorrectly.
automation.yaml
- alias: 'Monitor last action'
trigger:
platform: state
entity_id: light.pendant
action:
- service: shell_command.save_state
data_template:
value: '"{{trigger.entity_id}} changed to {{trigger.to_state}} at {{now().strftime("%X")}}"'
configuration.yaml
shell_command:
save_state: 'echo "{{ value }}" > /home/homeassistant/.homeassistant/shell_outputs/lastChange.sh'
I know that the automation is working. When I take a look at the debugs, I see this:
2017-07-18 20:10:32 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=shell_command, service=save_state, service_call_id=1979149808-16, service_data=value="light.pendant changed to <state light.pendant=off; friendly_name=Kitchen Pendant Lights, Zone ID=11, supported_features=1, Device ID=16 @ 2017-07-18T16:10:32.114499-04:00> at 16:10:32">
However, the shell_command doesn’t ever seem to be firing. My “lastChange.sh” file is completely empty. I also tried changing the shell_command to just: