I want a sensor that only conditionally runs a shell_command when the criteria device_tracker.pc is “home” and does nothing when it is “not_home”. I thought an automation that checks the device_tracker and then calls shell_command would work:
- id: '<id created by automation editor>'
alias: Run PC Uptime Sensor
description: ''
trigger:
- entity_id: device_tracker.pc
platform: state
to: "home"
condition: []
action:
service: shell_command.get_pc_uptime
But it does never fire at all. Is it because i’m looking for a state change? If yes, how can i have it just periodically check if the state is “home”? Or do i have to check for the state change initially and use a condition checking something like last_run for periodic runs? I’m guess i’m just not clear on the logic behind that.
This automation disables the previous automation when no one is ‘home’. Otherwise it enables it.
- alias: 'Control PC Uptime Automation'
trigger:
platform: state
entity_id: device_tracker.pc
action:
service_template: >
automation.turn_{{'on' if trigger.to_state.state == 'home' else 'off'}}
entity_id: automation.get_pc_uptime
An alternative approach is to use only one automation (Get PC Uptime) with a condition restricting its action only to when someone is ‘home’. However, the automation will continue to trigger every minute even when no one is ‘home’.
Thanks both of you, i came up with this based on the first reply (time_pattern + condition)
- id: 'id'
alias: Run PC Uptime Sensor
trigger:
- platform: state
entity_id: device_tracker.pc
to: home
- platform: time_pattern
minutes: /5
condition:
- condition: state
entity_id: device_tracker.pc
state: home
action:
- service: shell_command.get_pc_uptime
@123 I’m not sure i understand your last sentence. Are you refering to the automation itself running (testing the triggers and condition) every minute when noone is “home”? Is that bad?
Correct. Because it uses a Time Pattern Trigger, it will trigger according to its time interval. For example, when you are not home, your automation will still trigger every 5 minutes and evaluate the condition.
The example I provided restricts the automation (with the Time Pattern Trigger) to run only when you are home. If you’re not home, there’s no need to check every 5 minutes. It spares Home Assistant from performing a needless task.
Thanks for the explanation. Sadly, i just realized the whole thing doesn’t work out in the intended way.
Since shell_command.get_pc_uptime is just a script returning a value tied to a command_line sensor I think i just found an interesting way of … well … automating running that script. But I’d need to tell the sensor itself to not run. Or process the return value into a new sensor. Am I missing something really stupid or is it really that complicated? It sounds so simple: only update x when y is running
Ok, the sensor below is running regardless of the target machine status. This fills the log with errors, there are ways to suppress them, but the errors are correct. So my idea was to automate turning that sensor on and off.
# desktop pc uptime sensor
- platform: command_line
name: PC Uptime
command: "ssh [email protected] -p 22 -i /config/sshkey/id_rsa get_uptime.bat"
To be fair, i should have seen the problem a mile away. The return value just lands in the ether. But i was eager to learn that automation part and missed the forest for the trees.
Write a script that pings the remote server, and if it is online asks for the uptime. Use that script as your sensor. In fact you can probably do it in one line.
@nickrout Thanks, that’ll work more cleanly, but i wanted to do it with an automation independent from the target host’s script.
For posterity here is what i finally ended up with after some experiments is this. First i set the scan_invertal of the sensor to 10 years, effectively stopping it and then created an automation updating it based on the status of device_tracker. My whole installation got stuck combining the last two automations and couldn’t find any help on this. So after a while i just went with the two
- alias: Get PC Uptime
trigger:
platform: time_pattern
minutes: /5
action:
- data:
service: homeassistant.update_entity
entity_id: sensor.pc_uptime
- alias: Get PC Uptime Control Off
trigger:
- entity_id: device_tracker.pc
from: 'home'
platform: state
to: 'not_home'
action:
- data:
entity_id: automation.get_pc_uptime
service: automation.turn_off
- alias: Get PC Uptime Control On
trigger:
- entity_id: device_tracker.pc
from: 'not_home'
platform: state
to: 'home'
action:
- data:
entity_id: automation.get_pc_uptime
service: automation.turn_on
Yeah, and i even explained why. I don’t really see the problem? For me the solution was your example and I could not find any way around the “needlessly” longer part because i was unable to find a method even to troubleshoot it. My hass did not throw any errors for me to troubleshoot. Since someone marked mine as the solution now i guess that’s ok but i didn’t want to come off as touting my own horn presenting a marginally expanded version as the “solution”
Only you, the author of this topic, have the ability to select and mark a post as the Solution. Or are you claiming a forum moderator marked it without your knowledge or request?
For the record, there’s no functional difference between the single automation I posted and your double automations (other than twice as much code). Nevertheless, you are, of course, free to use whatever you prefer. Ultimately, the goal is to solve the problem and it was achieved.
Oh, now i understand. I did completely misunderstand you believing i had marked yours as the solution and instead marked mine from the get go. I then still failed to realize that when i was answering. For that i’m sorry, my english skills (and apparently quoting/scrolling capabilities) lack as much as my automation logic, it seems. Thank you, all good now - solution marked as it should have been.
Are you sure? For me that is a toggle and the only green solution icon is under post /145058/3 (Taras first post) If i click any other post it toggles to that.
It now appears corrected.
It may have been that my browser had cached the page (but I read your response, so VERY confused)
Regardless I appologise if this was the case.