FHEM Forum

FHEM => English Corner => Thema gestartet von: jacko am 06 Januar 2022, 20:25:39

Titel: How to read values into fhem from an external program?
Beitrag von: jacko 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.
Titel: Antw:How to read values into fhem from an external program?
Beitrag von: Otto123 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
Titel: Antw:How to read values into fhem from an external program?
Beitrag von: yersinia am 07 Januar 2022, 08:44:49
What about MQTT? On the client, you can install mosquitto-client (https://mosquitto.org/) and send any value as JSON string to your FHEM MQTT broker (https://fhem.de/commandref.html#MQTT2_SERVER) and save it in device (https://fhem.de/commandref.html#MQTT2_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 (https://fhem.de/commandref.html#MQTT2_DEVICE) will have an attribute readingList like
ID_of_your_client:tele/TOPICID/sysstat:.* { json2nameValue($EVENT) }and converts all JSON elements into readings.
Titel: Antw:How to read values into fhem from an external program?
Beitrag von: jacko 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.