Command line sensor manual update only

Hi!

I am trying to get the command line sensor to have scan_interval= never (I have it at a very long time at the moment) and then trigger it manually. Has anyone managed to?

So basically what I want to do is shell command but get the result to be able to know if/how the command worked. Not for debug but trigger other automations based on the repsonse

There’s no “never” value but you can set it to something very high. Other users have had a similar need and I suggested they do this:

Why not use a shell command , then fire it based on an automation where you can define a timestamp

This is what I do now but I would like to have a nicer solution. Not long time, but never…

I can not get any feedback from a shell command (that I am aware of).

A scan_interval of “never” doesn’t exist so any alternative solution will have to exclude the use of a command line sensor. I’m not sure the alternatives are “nicer”.

That is what I feared…

I will take this as an excuse to learn python better and hopefully add something to this great software.

Why not a shell command? That’s also a command line…

How does your sensor look like?

Because the shell command do not return anything.

Ow ok, I understand…

Maybe do a sensor that’s calls a python script , in that script you can define the date/time…

How about your command line sensor just reads the contents of a file? It can scan as often as it wants. Then you write a separate script outside of HomeAssistant or write a shell-command within HomeAssistant that writes its output to that file. Either way, it would only run when you want and the sensor would always report that data from the last run - even persisting across reboots.

I’ve just managed to disable polling of all command_line sensors (but it can be selective).
It’s a simple (a few strings) custom component that you only need for that workaround as you will still be using the standard (just patched) platform.
Let me know if it’s what you’re looking for.

Here is the working example.

Thank you!

Perfect, this is what I was looking for. Will have to try it when I have time fix any issues this kind of custom component might cause by tweaking the internals.

Does this solution still work? I see warnings about HA changes could break it. I am definitely interested.

What I really need is a Shell_Command with a return value, but I think a manually-triggered command line sensor will do the same.

Thanks!

I’m pretty sure it does. For decoding a sensor value I’m using a command line sensor to call a python script in order to do a calculation on a value I pass it and it then passes back a return value.
The only issue I have is some initial errors when it’s called after a startup and there’s no valid data to pass it so ideally need to only conditionally call the command if there’s valid input data. Example of my code as follows:-

sensor:
  - platform: command_line
    name: smartmeter_dp6
    command: "python /home/homeassistant/.homeassistant/python_scripts/base64_int.py {{ states('sensor.smart_meter_06') }}" 
  - platform: template 
    sensors: 
      smartmeter_voltage: 
       value_template: "{{(states.sensor.smartmeter_dp6.state.split('.')[0][0:4])|int(base=16)*0.1}}" 
       friendly_name: 'Volts'
       unit_of_measurement: "V"
      smartmeter_current: 
       value_template: "{{(states.sensor.smartmeter_dp6.state.split('.')[0][6:10])|int(base=16)*0.001}}" 
       friendly_name: 'Amps'
       unit_of_measurement: "A"
      smartmeter_power: 
       value_template: "{{(states.sensor.smartmeter_dp6.state.split('.')[0][12:16])|int(base=16)}}" 
       friendly_name: 'Watts'
       unit_of_measurement: "W"

I set scan_interval to zero and it seems to do the trick? Am I breaking something?

sensor:
  - platform: command_line
    command: 'curl -X POST -d "{\"Username\": \"xxxx\",  \"Password\" : \"xxxxxxxxxxxxxx\" }" http://192.168.x.xxx:9000/api/auth'
    name: "PortainerToken"
    scan_interval: 0
    value_template: "{{value[8:184]}}"  #remove first 8 characters and keep the next 176

rest_command:
  plextraktsync:
    url: http://192.168.x.xxx:9000/api/endpoints/1/docker/containers/xxxxxxxxxxxxxxxxxxxxx/start
    method: POST
    headers:
      Authorization: "{{states('sensor.portainertoken')}}"
#  scan_plex_tv_shows:
#    url: "http://192.168.x.xxx:xxxxxxx/library/sections/1/refresh?X-Plex-Token=xxxxxxxxx"

The output of the command_line is an auth token, which stored as a sensor, which used in a rest_command.

I then use an automation to manually reload it and run the rest_command

alias: Sync Plex & Trakt
description: ''
trigger:
  - platform: time
    at: '06:00'
condition: []
action:
  - service: command_line.reload
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: rest_command.plextraktsync
    data: {}
mode: single