Autor Thema: How to read values into fhem from an external program?  (Gelesen 1088 mal)

Offline jacko

  • New Member
  • *
  • Beiträge: 18
How to read values into fhem from an external program?
« am: 06 Januar 2022, 20:25:39 »
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/analog.py"
or
{ fhem `/opt/fhem/scripts/analog_with_set_cmd_output_as_well.py` }
But these just set ADC to the string "/opt/fhem/scripts/analog.py" or similar!

The only way I can make this happen is to create a bash script containing
echo set ADC `/opt/fhem/scripts/analog.py` | ncat localhost 7072This 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.

Offline Otto123

  • Tester
  • Hero Member
  • ****
  • Beiträge: 20980
  • schon mal restore trainiert?
    • Otto's Technik Blog
Antw:How to read values into fhem from an external program?
« Antwort #1 am: 06 Januar 2022, 20:55:43 »
Hi jacko,

there would be a way directly with python https://wiki.fhem.de/wiki/CsrfToken-HowTo#Python
Or there would be a way over HTTP https://github.com/heinz-otto/fhemcl

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

Otto
Viele Grüße aus Leipzig
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Offline yersinia

  • Hero Member
  • *****
  • Beiträge: 1386
    • Cyanide & Happiness
Antw:How to read values into fhem from an external program?
« Antwort #2 am: 07 Januar 2022, 08:44:49 »
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:
#!/bin/bash

mqtt_host="FHEM-IP"
mqtt_port="FHEM-MQTT-SERVER-PORT"
mqtt_user="FHEM-MQTT-CLIENT-USER"
mqtt_pass="FHEM-MQTT-CLIENT-PSWD"
mqtt_clientid="ID_of_your_client" #give it a name
mqtt_topicid="TOPICID" # the topic ID you want to react on in FHEM

jsontxt='{'
#date date +%FT%T
        jsontxt+='"time":"'
                jsontxt+=$(date +%FT%T)
        jsontxt+='",'
#host name
        jsontxt+='"hostname":"'
                jsontxt+=$(hostname)
        jsontxt+='",'
#kernel version
        jsontxt+='"kernel_version":"'
                jsontxt+=$(uname -r)
        jsontxt+='",'
#[...]
jsontxt+='}'
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.1 (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

Offline jacko

  • New Member
  • *
  • Beiträge: 18
Antw:How to read values into fhem from an external program?
« Antwort #3 am: 07 Januar 2022, 12:42:36 »
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.