Last Change Custom Sensor

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”.

Is this possible?

Thanks!

1 Like

There’s no “text input” sensor in HA. I have worked around this on another project by using a text file to store the date.
The process is documented here: https://community.home-assistant.io/t/alexa-roomba-and-presence-detection/18763

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")"}}'
shell_command:
  saveState: echo {{text}} > <full_path_to_your_file>/status.sh
sensor:
  - platform: command_line
    name : lastChange
    command: <full_path_to_your_file>/status.sh
    scan_interval : 1
1 Like

I think this would be fairly easy with AppDaemon if you’re willing to give it a try (assuming you haven’t already).

@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 :confused: 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:

save_state: echo 'test'  > /home/homeassistant/.homeassistant/shell_outputs/lastChange.sh

and that works just fine.

Thanks for any help you can give me!

Everything I am reading says this should work, but I can’t make it work either.

Ok, I’ve been playing around for the past 4 hours and I finally got it figured out:

configuration.yaml

shell_command:
  save_state: bash -c 'echo {{ value }} > /home/homeassistant/.homeassistant/shell_outputs/lastChange.sh'

automation.yaml

- alias: 'Monitor last action'
  trigger:
    platform: state
    entity_id: light.pendant
  action:
    - service: shell_command.save_state
      data_template:
        value: "echo {{trigger.to_state.attributes.friendly_name}} changed to {{trigger.to_state.state}} at{{now().strftime('%l:%M%p %Z on %b %d, %Y')}}"

The most important part is the bash -c part. Make sure to put the command you want to execute in single quotes.

Thanks for the help!

3 Likes