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.

2 Likes

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"

Interestingly, I noticed (at How to access core and supervisor CPU + MEM usage (HA OS) - #6 by e-raser) my CL sensors seem to only update when

  • manually reloading CL sensors
  • HA starts (of course, as all sensors are initialized)
  • using the homeassistant.update_entity service

Many of them seem to ignore the scan_interval, at least as discovered at How to access core and supervisor CPU + MEM usage (HA OS) - #6 by e-raser.

  • Has there been any change for CL sensors back in 2022?
  • Do we now need to use the homeassistant.update_entity service?
1 Like