Template trigger: LastActive
alias: example
trigger:
- platform: template
value_template: "{{ (now() - states('sensor.testvm_lastactive')|as_datetime).total_seconds() < 60 }}"
action:
... etc ...
alias: example
trigger:
- platform: template
value_template: "{{ (now() - states('sensor.testvm_lastactive')|as_datetime).total_seconds() < 60 }}"
action:
... etc ...
Converting to milliseconds should work for you.
($millis() - $toMillis($entity().state)) < 6000
[{"id":"07b9df517f9d0f29","type":"server-state-changed","z":"0a325c35fc29f44e","name":"","server":"","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.testvm_lastactive","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"($millis() - $toMillis($entity().state)) < 6000","ifStateType":"jsonata","ifStateOperator":"jsonata","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":480,"y":7160,"wires":[[],[]]}]
Thank you. But you know what confuses me? It doesn’t do anything, even if I just tested as a regular automation rather than in node red
I thought this was supposed to trigger some thing once I use my PC or apparently if I’m interpreting the 60 seconds value correctly then if I have used my PC less than 60 seconds ago
i have the code from this page. Misc Examples - HASS.Agent it contains no explanation though
Try unchecking the box Current state equals previous state
. You can also go over to devtools, template tab and enter the value template to make sure you get a result.
The next example shows how to setup a template binary sensor in HA. It may be easier to have an entity as it could be used in current state nodes.
template:
- binary_sensor:
- name: "User Active"
state: "{{ as_timestamp(now(),0) - as_timestamp(states('sensor.staging_lastactive'),0) < 60 }}"
The regular automation will trigger when the template goes from false to true.
You don’t say what that sensor is, but that template change will occur when the datetime string in the sensor is updated from a value that is earlier than a minute in the past to a value that is later than a minute in the past.
Here’s my flow hass.agent.
It creates a binary sensor that is set to true
when lastactive
is updated and if it isn’t updated again within 30 seconds it sets it to false
. I have an extra check in the trigger state node that checks if I’m in the room.
[{"id":"314065a05bbaf6a2","type":"group","z":"1222406.8e2dfc","name":"ryzen in use","style":{"label":true},"nodes":["edde09fc0cb798cd","4778701d707e3d13","6a9f68e88ffafa0c"],"x":5,"y":2359,"w":886,"h":82},{"id":"edde09fc0cb798cd","type":"ha-binary-sensor","z":"1222406.8e2dfc","g":"314065a05bbaf6a2","name":"","entityConfig":"59647e6f49d530fb","version":0,"state":"payload","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":785,"y":2400,"wires":[[]]},{"id":"4778701d707e3d13","type":"trigger","z":"1222406.8e2dfc","g":"314065a05bbaf6a2","name":"","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":459,"y":2400,"wires":[["edde09fc0cb798cd"]]},{"id":"6a9f68e88ffafa0c","type":"trigger-state","z":"1222406.8e2dfc","g":"314065a05bbaf6a2","name":"","server":"","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.ryzen_lastactive","entityIdType":"exact","debugEnabled":false,"constraints":[{"targetType":"entity_id","targetValue":"sensor.jason_room","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"office"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":181,"y":2400,"wires":[["4778701d707e3d13"],[]]},{"id":"59647e6f49d530fb","type":"ha-entity-config","server":"2dad33ee.42bf5c","deviceConfig":"","name":"Ryzen in use","version":"6","entityType":"binary_sensor","haConfig":[{"property":"name","value":"Ryzen in use"},{"property":"icon","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""}],"resend":false}]
unchecking the box didn’t do anything
I added the template sensor but it’s permanently set to off even though MQTT is running in HASS.Agent. The original lastActive sensor by Hass.Agent also reports effortlessly.
I tried rebooting my host device but that didn’t help.
Not sure if I get what you’re saying and what you’re referring to.
it says this node needs a node red costum version to work which I’d prefer not to install, considering the thing I’m asking for is, correct me if I’m wrong, possible to do with less nodes and installing new things in Home Assistant always brings a bunch of unexpected problems
You can use the same flow and swap out the sensor node for a call service that turns on/off a input boolean created inside Home Assistant.
do you mean the on in the developer tools by “template tab”?
I tried using it and noticed the binary template sensor refers to a sensor that isn’t in my frontend’s sensor list so I assume it’s wrong and changed it to how my LastActive sensor was named and rebooted.
Didn’t change anything. Same with the template trigger from my OP. Any other ideas?
I don’t get what you mean since there’s no node called “input boolean”.
I don’t get how an input boolean is gonna help me in this situation. And I honestly don’t have time for guessing games like that.
Seemed clear enough to me: I was just helping out with what an Input Boolean is, as the UI calls it a Toggle Helper.
My sincerest apologies that our attempts to help you are taking longer than you’d like.
You are trying to create a trigger to start an automation. With the input boolean, you would use that entity to trigger an automation. Create an input boolean go to settings → devices and services → helper tab at the top → +add bottom right corner → create toggle(input boolean).
The entity will be input_boolean.what_you_named_it
, put that entity in the call service.
[{"id":"fdc8f3eeaebb6cb4","type":"api-call-service","z":"0a325c35fc29f44e","g":"314065a05bbaf6a2","name":"","server":"","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_{{payload}}","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":870,"y":7500,"wires":[[]]}]
Now to start an automaton use that input boolean as the trigger entity.
This variant does in fact work. I didn’t realize I had to rename the sensor name to that of lastactive as it’s named in my HA.
I’m struggling to make another trigger for 30m inactivity though.
Use the input boolean in an event state node. You can set a time condition(For).
[{"id":"7820fcca5586b59d","type":"server-state-changed","z":"a5fc1698.8bc088","name":"Used PC within the last 30m?","server":"9322cd47.1111","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.ars_computer_ars_computer_lastactive","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"($millis() - $toMillis($entity().state)) < 180000","ifStateType":"jsonata","ifStateOperator":"jsonata","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":660,"wires":[[],["d30b35898f6f22bd"]]},{"id":"d30b35898f6f22bd","type":"api-call-service","z":"a5fc1698.8bc088","name":"Activate Sleep Mode","server":"9322cd47.1111","version":5,"debugenabled":false,"domain":"button","service":"press","areaId":[],"deviceId":[],"entityId":["button.ars_computer_computer_ruhezustand"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":420,"y":680,"wires":[[]]},{"id":"9322cd47.1111","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
Did I write this correctly?
It just spam-activated unintentionally, I don’t know why. It’s your script but with > reversed and a different number of seconds.
Connect the call service to the top output instead of the bottom.