Command line sensor timeout (Solved by setting up ruuvitag sensor to update HA via REST API)

Hi! I ran into a problem with the default timeout setting. I tried searching for forums a way around it and haven’t found anything so far. Default timeout is probably around 15-20 seconds. I tried to manually set timeout for a sensor but there doesn’t seem to be a feature for that. Is that something I could edit easily from source code?

My sensor is a python script that scans three Ruuvitag weather sensors via bluetooth. The script works and it takes about 23 seconds to complete. I have successfully scanned one sensor which takes about 11 seconds and timeout isn’t a problem in that case. I could try configuring a sensor for each of these, but in my case it would be easier to get the info from all the sensors and after filter it with a template sensor. My raspberry is years old and slow, but this is the first time it gives me a real problem.

My sensor code:

  -  platform: command_line
     name: ruuvitags
     command: "python3 /home/homeassistant/ruuvi-hass/"
     scan_interval: 300
     timeout: 60

Error log:

2018-05-24 13:53:13 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.ruuvitags is taking over 10 seconds
2018-05-24 13:53:35 ERROR (Thread-4) [homeassistant.components.sensor.command_line] Timeout for command: python3 /home/homeassistant/ruuvi-hass/

My gut reaction is you might want to consider running the script another way, say as a cron job, and have it write the values to a file. Then have your HA sensor just read the most recent values from the file.

Thanks for the reply! I actually started thinking about that right after posting here. I will take a look into it. Looks like the File Sensor component is the thing to try here.

Yep, just saw someone else mention the file sensor. Or I understand that MQTT is a popular method, too. I haven’t tried either yet, though, so can’t really comment further about either.

1 Like

You could use the rest api of HA and even run the python script as a crons job every 5 mins. I do that to process some security camera images before HA consumes it.

Set the state of an entity
Of course, it’s possible to set the state as well:

import homeassistant.remote as remote
from homeassistant.const import STATE_ON

api = remote.API('', 'YOUR_PASSWORD')
remote.set_state(api, 'sensor.office_temperature', new_state=123)
remote.set_state(api, 'switch.livingroom_pin_2', new_state=STATE_ON)

Thanks! This also seems like something to try. I’ll have to read more about REST.

Thanks for the tips. I decided to read about REST API Python bindings and that actually does the job quite nicely after some initial setup. I spent quite some time thinking what kind of sensor to put in the HA conf file for dumping the data via rest but then I found out it doesn’t need any sensors and the rest makes the sensors on the go and with couple attributes formatting can be set. So now I have a python script running every five minutes triggered by cron.

If anyone has a need for this, here’s a quick guide :slight_smile:

The things needed:

  • ruuvitag and home assistant python modules installed (I decided to run everything as the homeassistant user and use the ha environment as source so some copying of modules was required.
  • Sudo rights for bluetooth services for ha user. (use “sudo visudo”)
homeassistant   ALL = (ALL) NOPASSWD: /bin/hciconfig, /usr/bin/hcitool, /usr/bin/hciattach, /usr/bin/hcidump, /usr/bin/hcitool, /bin/kill

And here is my python script (I left just one sensor for this example, but it’s easy to replicate the lines with new variables:

import sys
import time
import os
import homeassistant.remote as remote
from homeassistant.const import STATE_ON
from datetime import datetime
from ruuvitag_sensor.ruuvitag import RuuviTag

api = remote.API('https://your.homeassistant:port/api', 'Your_api_password')

# Change here your own device's mac-address
mac = 'XX:XX:XX:XX:XX:XX'

sensor = RuuviTag(mac)

data = sensor.update()

remote.set_state(api, 'sensor.temperature', new_state=data['temperature'],attributes={'friendly_name': 'Living room temperature', 'unit_of_measurement': '°C'})
remote.set_state(api, 'sensor.pressure', new_state=data['pressure'],attributes={'friendly_name': 'Living room air pressure', 'unit_of_measurement': 'hPa'})
remote.set_state(api, 'sensor.humidity', new_state=data['humidity'],attributes={'friendly_name': 'Living room humidity', 'unit_of_measurement': '%'})