Hi,
which is the easiest way to get json formatted data like this {"diesel":1.1,"super":1.32,"autogas":0.51}
into a device with attributes?
json to device
Hi,
which is the easiest way to get json formatted data like this {"diesel":1.1,"super":1.32,"autogas":0.51}
into a device with attributes?
pimatic-google-calendar | pimatic-wmi | pimatic-snmp | pimatic-wakeonlan |
Like my work? Then consider a donation
Follow me: www.thorstenreichelt.de
The part of the string you show is is not a complete json data string.
you could use the json command (npm install json)
I do that for pimatic itself like
curl -X GET --silent --user "user:password" -s http://localhost/api/variables/tuin-temperatuur
which gives me
{"variable":{"name":"tuin-temperatuur","readonly":false,"type":"value","value":20.3,"unit":""},"success":true}
So I use:
curl -X GET --silent --user "user:password" -s http://localhost/api/variables/tuin-temperatuur | json variable.value
which gives me:
20.3
(And less split out):
curl -X GET --silent --user "user:password" -s http://localhost/api/variables/tuin-temperatuur | json variable
{
"name": "tuin-temperatuur",
"readonly": false,
"type": "value",
"value": 20.3,
"unit": ""
}
so for example:
curl -X GET --silent --user "user:password" -s http://localhost/api/variables/tuin-temperatuur | json variable.readonly
false
Your json string is not complete. It should say something like
{"<your discriminator for json>":{"diesel":1.1,"super":1.32,"autogas":0.51}}
Or via MQTT protocol:
"attributes": [
{
"name": "diesel",
"topic": "data/json",
"type": "number",
"unit": " €",
"acronym": "diesel",
"messageMap": {}
},
{
"name": "super",
"topic": "data/json",
"type": "number",
"unit": " €",
"acronym": "super",
"messageMap": {}
},
{
"name": "autogas",
"topic": "data/json",
"type": "number",
"unit": " €",
"acronym": "autogas",
"messageMap": {}
}
],
"xAttributeOptions": [],
"id": "sensor",
"name": "Sensor",
"class": "MqttSensor"
}
Pimatic = Smart Home
if all fails, a logreader device or a shellsensor could help
pimatic v0.9 has been released!
Support Pimatic and get some free stickers
Like us on Facebook
make it so !
Do I have to install some additional software for this?
If i do this
curl -X GET --silent --user "USER:PASSWD" -s http://192.168.178.20/api/variables/CV_waterdruk | json variable.value
I get this:
-bash: json: opdracht niet gevonden
(whitch means json: command not found)
Installed jsontool, but to no avail…
sudo npm install json
EDIT:
Sorry. should be.
cd ~
npm install json
sudo npm link
Thank you! That’s indeed what did the trick!
@wutu I Know this is an old post, but I cannot seem to find out what you mean. I get an mqtt topic from my tv that gives me the followin payload in Json form. How can I get only certain values to a variable of mqttsensor? I would like to get the channelNumber and the channelName
{
"val": "8",
"lgtv": {
"subscribed": true,
"channelId": "3_33_8_8_2100_19442_1536",
"signalChannelId": "2100_19442_1536",
"channelModeId": 1,
"channelModeName": "Cable",
"channelTypeId": 4,
"channelTypeName": "Cable Digital TV",
"channelNumber": "8",
"channelName": "Veronica HD / Disney XD",
"physicalNumber": 33,
"isSkipped": false,
"isLocked": false,
"isDescrambled": false,
"isScrambled": true,
"isFineTuned": false,
"isInvisible": false,
"isHEVCChannel": false,
"favoriteGroup": null,
"hybridtvType": "HBBTV",
"dualChannel": {
"dualChannelId": null,
"dualChannelTypeId": null,
"dualChannelTypeName": null,
"dualChannelNumber": null
}
}
}
Hey @JahFyahh
Use lgtv.channelName
as a name for attribute.
More info: https://www.npmjs.com/package/flat and https://forum.pimatic.org/topic/1865/kodi-s-and-mqtt.
Pimatic = Smart Home
@wutu Thank you for your reply. I already figured that by using a python script to get the attribute from the payload and curl it to an pimatic variable. but is there a way to make pimatic retreive the attributes payload without invloving an external python script?
@wutu Never mind, I overlooked somethng in the link and found out how to do it. Thank you.
@JahFyahh can you explain how?
@temp Using the link: https://forum.pimatic.org/topic/1865/kodi-s-and-mqtt. I realised that lgtv.channelNumber has to be the name of an mqttsensor attribute. (I was overthinking it)
Using GUI
I also use the python version, because I wanted to add an kodi thumbnail to an iframe. But the kodi2mqtt changes ASCII encoding in the url in the attribute. So I created an empty iFrame called kodi-thumb and use a rule to load the iframe url with an variable which is populated with the python script:
import paho.mqtt.subscribe as subscribe
import json
import os
pimatic_server = '’
pimatic_user = '’
pimatic_pass = ‘**************’
pim_user_pass = pimatic_user + ‘:’ + pimatic_pass
curl_prefix = 'curl --silent --insecure -X PATCH --header “Content-Type:application/json” --data '{“type”: “value”, “valueOrExpression”: "'
pim_server_url = pimatic_server + ‘/api/variables/kodithumb’
def on_message_print(client, userdata, message):
#print("%s %s" % (message.topic, message.payload))
obj = json.loads(message.payload)
#print “Value : %s” % data[‘val’]
value = obj[‘kodi_details’][‘thumbnail’]
value = value.replace(“image://”, “”)
value = value.replace("/", “”)
value = value.replace("%2f", “/”)
value = value.replace("%3a", “:”)
os.system(curl_prefix + str(value) + ‘"}’ --user “’ + pim_user_pass + '” ’ + pim_server_url)
subscribe.callback(on_message_print, “hombmc/status/title”, hostname=“172.0.0.1”)
The Rule:
Kodi State of customkodi5 is equal to “play” for 1 minutes then load kodi-thumb with “$kodithumb”
If someone knows a cleaner way to get this done within pimatic without external scripts I would love to know.