How to initiate an on-demand sensor update from a script or automation?

I’m using a custom sensor component to provide reverse geocode lookups from OpenStreetMap.
It updates itself periodically and seems to work okay for the most part - but it seems to update itself on it’s own schedule and sometimes lags behind the device_tracker location.

So here’s the problem:

I have an automation which is configured as shown below. It triggers off of a device_tracker change - but the notification messages contain information from both the device_tracker entity AND the separate sensor entity - and the sensor entity’s attributes are not up to date at the time the event is triggered. Is there any way I can call a sensor.update() method from my automation to force the sensor to perform an update “on demand” instead of waiting for it to update itself based on it’s own throttling settings? :

- alias: LocationDaughter
  trigger:
    platform: state
    entity_id: device_tracker.daughter_daughter
  action:
  - service: notify.androidtv_office
    data_template:
      title: "Daughter: {{ states.device_tracker.daughter_daughter.state }}"
      message: >
        {{ states.sensor.daughter.attributes["Place Name"] }} / {{ states.sensor.daughter.attributes["Formatted Address"] }}
  - service: notify.ios_myiphone
    data_template:
      title: "Daughter: {{ states.device_tracker.daughter_daughter.state }}"
      message: >
        Daughter: {{ states.sensor.daughter.attributes["Place Name"] }} / {{ states.sensor.daughter.attributes["Formatted Address"] }} @ {{ now().strftime("%Y-%m-%d %H:%M") }}
      data:
        push:
          category: map
        action_data:
          latitude: '{{ states.device_tracker.daughter_daughter.attributes.latitude }}'
          longitude: '{{ states.device_tracker.daughter_daughter.attributes.longitude }}'
          second_latitude: '{{ states.device_tracker.son_son.attributes.latitude }}'
          second_longitude: '{{ states.device_tracker.son_son.attributes.longitude }}'
          shows_traffic: true
          shows_points_of_interest: true
          shows_line_between_points: true
#          shows_user_location: true

Thanks

Did you ever figure this out? I am trying to do the same thing

No, not exactly - but I did find a workaround.

What exactly are you trying to do?

I’m trying to update sensor from a script. I have 4 sensors that read from the same source (but different commands) and I need to have at least 5 seconds between calls. Tried scan_interval, but it doesn’t work well with multiple sensors. Here is what I need to happen:

Command Line Sensor 1 - Update State
Wait 5 seconds
Command Line Sensor 2 - Update State
Wait 5 seconds
Command Line Sensor 3 - Update State
Wait 5 seconds
Command Line Sensor 4 - Update State
Wait 30 seconds
Repeat

So - you only want this to run “on demand” and not “periodically”?

My problem was easier to solve because I was working on a custom component and was able to subscribe to “state changed” events from the component I was dependent on and then generate my own state change event after updating and trigger my automation off of it.

I tossed around a few ideas for how to handle your situation and can’t think of any way to handle it without updating the sensor code itself. Have you thought about writing a Python script that performs this update cycle periodically and attach it to a sensor? You could store all 4 sensor values as attributes of the one new sensor - and then use value template sensors to get their data from the new one you create. I’ve never done that - but I think it’s possible and I can’t think of a better way to handle your situation. Perhaps if you post it as a new question, some folks with much more HA experience will know of a more straightforward way to do it.

Sorry I couldn’t be of more help.

Actually, I do want to run then periodically (every 30 seconds) but I need the calls to be sequential, and 5 seconds between them. Probably helps if I explain what I am trying to do as a whole, so here it goes… I have WiFi Legrand Adorne switches/dimmers (see here), and the only way to control them (without using their hub) through automation is to use their RS232 -> RF Interface (see here)

So what I have done is attached an iTach Flex to the serial port, then use netcat to send various commands (see their protocol guide here) For instance, this command line sensor gets the current level in the Master Bedroom (Group 3760):

  - platform: command_line
    command: >-
      /bin/echo -e "STSG 3760\r" | netcat 192.168.0.49 4999 -w2
    name: Master Bedroom Recessed Lights Level
    #unit_of_measurement: "%"
    scan_interval: 10
    value_template: '{{ (value.split(", 0")[1] | int / 2.55) | round(0) }}'

This is the request/response:

>STSG 3760
>GS, 3760, 0153, 0153, 0065, 0051

3760 is the light group, 0153 is the brightness from 1-255.

To set the light levels, I use shell commands:

master_bedroom_lights_40: /bin/echo -e "RAMPR,1,102,50\r" | netcat 192.168.0.49 4999 -w2

Which sets Master Bedroom lights to 40%.

I have 3 lights groups currently (Master Bedroom, Master Bathroom, Hallway) so when it tries to either get or set levels, there needs to be 5 seconds between each call, or the RF interface goes to not responding and has to be restarted

I have looked at quite a few default “light” components to try and make a custom_component, but wouldn’t even know where to start. I also don’t know how I would get the levels of the 3 groups in one call (and one sensor) using a command line sensor.

There is a plugin for Vera that does work with the RF interface, but relaying it through home assistant --> Vera --> RS232 RF Interface while trying to turn lights on with motion is not so great.

Hi Cody,

To be honest, I don’t have any experience or familiarity with any of the devices you are using - but it sounds to me like you’re going to need to treat them as if they were a single device - at least for the sake of your updates that need to be 5 seconds apart.

To tackle this now, with my current knowledge level, I’d take the shell commands that you are calling separately and I’d put them into a bash shell script. Then I’d redirect the output of each one of the commands to a text file on the file system, then I’d put a sleep for 7 seconds after each command. So - it would take 21-ish seconds for your script to run and gather data from all 3 lights. Before you end your script, you may want to parse the output of your 3 separate files and extract the exact data you need for your sensor and put it in a another text file in which you can easily “grep” the data you need for your sensor from it with a single command.

With the above script in place - I’d create one command line sensor to run your bash script every 30 seconds - and 3 more command line sensors each of which will grep their required data from the text file your bash script creates (rather than communicating with your hardware directly).

As I said in my previous reply - others may have better ideas - but that’s how I’d approach it with what I know today. Hope it helps.

1 Like

For sure helps! I’ll give that a try. My workaround currently is when HA starts up, I turn all 4 sensors off, then have a 6 second delay, turn the first on, another 6 second delay, turn the second one on, etc. and set scan interval to 24 seconds on all of them. It works (surprisingly) well, but it’s messy, I like the out to file and grep idea better. I’ll let you know how it goes

Thanks,
Cody

Ok maybe not, I thought it was working, but you can’t turn sensors on/off apparently… :frowning:

The homeassistant.update_entity service probably should work.
(Writing to an old thread because it is the first google result on the subject)

2 Likes