How to read values into fhem from an external program?

Begonnen von jacko, 06 Januar 2022, 20:25:39

Vorheriges Thema - Nächstes Thema


I have a simple linux/bash command line program that reads an analogue port and outputs the voltage. I would like this voltage available inside fhem. I have tried a number of combinations of the shell commands in the FHEM Command types entry but can't get anything to work  :-[. For example
set ADC "/opt/fhem/scripts/"
{ fhem `/opt/fhem/scripts/` }

But these just set ADC to the string "/opt/fhem/scripts/" or similar!

The only way I can make this happen is to create a bash script containing
echo set ADC `/opt/fhem/scripts/` | ncat localhost 7072
This seems quite cumbersome requiring the telnet port to be enabled/open and ncat to be installed on the system.

As this seems to be an obvious thing to want to do I am hoping that someone can provide some simple fhem examples to get me going.

Any help would be appreciated.


Hi jacko,

there would be a way directly with python
Or there would be a way over HTTP

That prevents you from define telnet port 7072 - but not really easier  ;)

Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz


What about MQTT? On the client, you can install mosquitto-client and send any value as JSON string to your FHEM MQTT broker and save it in device.

The JSON string can be generated through a bash script as well - and via cronjob executed regularly. I use it to send some pi values to (another) FHEM pi:

mqtt_clientid="ID_of_your_client" #give it a name
mqtt_topicid="TOPICID" # the topic ID you want to react on in FHEM

#date date +%FT%T
                jsontxt+=$(date +%FT%T)
#host name
#kernel version
                jsontxt+=$(uname -r)
mosquitto_pub -h $mqtt_host -p $mqtt_port -u $mqtt_user -P $mqtt_pass -i $mqtt_clientid -t tele/$mqtt_topicid/sysstat -m "$jsontxt"

The FHEM MQTT Device will have an attribute readingList like
ID_of_your_client:tele/TOPICID/sysstat:.* { json2nameValue($EVENT) }
and converts all JSON elements into readings.
viele Grüße, yersinia
FHEM 6.3 (SVN) on RPi 4B with RasPi OS Bullseye (perl 5.32.1) | FTUI
nanoCUL->2x868(1x ser2net)@tsculfw, 1x433@Sduino | MQTT2 | Tasmota | ESPEasy
VCCU->14xSEC-SCo, 7xCC-RT-DN, 5xLC-Bl1PBU-FM, 3xTC-IT-WM-W-EU, 1xPB-2-WM55, 1xLC-Sw1PBU-FM, 1xES-PMSw1-Pl


Thank you for the excellent solutions. I am relieved that I hadn't missed something in the FHEM Command Reference that would enable me to do this directly  :).

Although I use mqtt extensively to communicate between my IOT systems I am using the fhemcl solution as this is local and doesn't rely on my mqtt broker.

The rapid response, as always, is greatly appreciated.