No announcement yet.

Execute a script every time a temperature sensor changes value?

  • Filter
  • Time
  • Show
Clear All
new posts

    Execute a script every time a temperature sensor changes value?

    I would like to run a, preferably Linux bash or Python script, every time a temperature sensor changes value passing the new temperature as an argument - can somebody propose the simplest way of doing this on the HS3-Pi?!

    What I REALLY would have liked to do is to update an environment variable in the Pi with the temperature value read from the sensor (this is what my script would do) so that another periodic script (run by crontab) that is polling a heat pump (using a REST service API) also could include the current indoor temperature in the log entries it sends to the AWS cloud (where I analyze it further).

    The only solution I have thought about so far is to use a plugin that send the HomeSeer log to syslog, direct the syslog to a minimal syslog server local on the Pi (there are 20 line Syslog implementations in Python that I could modify) that would parse the log entries as they are received and update the environment variable as soon as it finds entries related to temperature readings - was however hoping there may be a simpler way....

    I'm sure there's a better solution considering you're running it on a Pi and with that have limited oomph.

    But I personally would favor (mcs)MQTT and bring it into NodeRed and from there you can do pretty much anything you want.
    (for example, I use it to log values from sensors into a InfluxDB which I use Grafana to display/analyze)

    Not sure what parts of AWs you're using, but AWS IoT seem to support MQTT directly (without the need for NodeRed).


      Thanks for the suggestion - I could also use AWS Greengrass or AWS IoT MQTT library (imho good alternatives to NodeRed) to send the data.

      What I would like here is however to only use "free tier" services in AWS (DynamoDB has a generous free tier for both storage and inserts/reads that never expire!) and directly in the PI get both the current indoor temperature and the heat pump readings into one and the same record that I send directly to DynamoDB in AWS rather than sending them separately and having to merge them in AWS somehow (doable with a Lambda function but would require more work and possibly result in service usage that is not free).

      I am not aware any Homeseer API that would allow me to ask for ZWAVE temperature sensor values from the Pi shell script that once every 5 minutes is run by cron (making the REST call to read the heat pump sensor values) or else this could be an alternative but as it is now I can only get the temperature sensor reading INSIDE of Homeseer and the heatpump readings outside and need to fins a way to bring them together...


        Three thoughts on how to do this.

        1. Use the HS JSON api to get the status from your scripting language of choice. This will return a JSON response. replace the <refId> with the HS reference ID for the device containing your z-wave temperature.
        This will return a JSON response as follows which needs to be parsed.
        {"Name":"HomeSeer Devices","Version":"1.0","Devices":[{"ref":336,"name":"Temperature","location":"HVAC","location2":"1st Floor","value":70.0,"status":"70 F","device_type_string":"Z-Wave Temperature","last_change":"\/Date(1578067369889-0600)\/","relationship":4,"hide_from_view":false,"associated_devices":[24],"device_type":{"Device_API":16,"Device_API_Description":"Thermostat API","Device_Type":2,"Device_Type_Description":"Thermostat Temperature","Device_SubType":1,"Device_SubType_Description":"Temperature"},"device_type_values":null,"UserNote":"","UserAccess":"Any","status_image":"/images/HomeSeer/status/Thermometer-70.png","voice_command":"","misc":4352,"interface_name":"Z-Wave"}]}
        You can then do what you need after obtaining the temperature from the z-wave device.

        You call the script in HS as follows. (yep, sometimes I can't spell when I'm coding).
        Click image for larger version

Name:	Capture.PNG
Views:	301
Size:	119.5 KB
ID:	1351520

        Now, I'm not certain how you dynamically set a global environment variable in linux. I though setting environment variables within a script was local to that script and any sub-scripts that the main script shells out to. An alternative would be to write a file to /tmp which your cronjob reads. /tmp is a temp file system and is loaded into memory and does not write to the SD card.

        2. Based on my interpretation of what you are wanting, I recommend the following. Your crontab script could use the JSON api directly as outlined in #1 above and query the HS device status as needed. There would be no need to set an environment variable nor the HS event on device change.

        3. You could leverage the ASCII api vs the JSON api. It's a bit more involved programatically. Refer to regarding both API's. Your script would need to open a tcp socket to port 11000. You will receive entries when any HS device changes. Your script would need to filter for the device(s) you are interested in and set your global environment variable(s) or /tmp(s) file for cronjob processing.

        Sample output using telnet.
        Click image for larger version

Name:	Capture1.PNG
Views:	201
Size:	28.1 KB
ID:	1351522
        Attached Files


          Thanks - did not know this API existed - this sounds like just what I wanted!


            I can confirm that the JSON API worked very well. To extract the temperature "value" of my temperature sensor with reference number 266 I used this "bash script line":

            curl 2>/dev/null "http://<homeseer IP>/JSON?request=getstatus&ref=266" | jq .Devices[0].value

            where curl is used to call the Homeseer web service and "jq" is used to process the JSON result to return only the temperature.