I am using the Spotify binding and wants to make a rule to transfere the play back between different devises. Unfortunately, the id of devises seems to change after some time. Therefore I want to read the devise id from a json path, to ensure that I have the latest id.
I manage to get the id from the json, but the string Always includes [ ] - like id =[“2546d8ce80580c473293d30e2c3477afaf985613”]
The first 3 lines in the rule below works, and thereafter I am testing to eliminate the [ ], but this do not work. How can I eliminate the [ ]?
val newValue = transform("JSONPATH", "$.[?(@.name=='Kitchen')]id", spotify_device_list.state.toString)
spotify_device_ID_Kitchen.postUpdate( newValue )
val newValue2 = transform("JSONPATH", "$.[?(@.name=='SHIELD')]id", spotify_device_list.state.toString)
val newValueX = transform("JSONPATH", "$.[?(@.name=='SHIELD')]id", spotify_device_list.replace('[','').replace(']',''))
val newValue3 = (newValue2.replace('[', '').replace(']', ''))
spotify_device_ID_SHIELD.postUpdate( newValue3 )
spotify_device_ID_Kitchen.postUpdate( newValueX )
spotify_current_device_id.sendCommand(spotify_device_ID_SHIELD) //Shield
Thread::sleep(50)
spotify_action.sendCommand("transfer_playback")
vzorglub
(Vincent Regaud)
May 23, 2018, 6:48am
2
Try that and monitor the log
val newValue = transform("JSONPATH", "$.[?(@.name=='Kitchen')]id", spotify_device_list.state.toString)
spotify_device_ID_Kitchen.postUpdate( newValue )
val String newValue2 = transform("JSONPATH", "$.[?(@.name=='SHIELD')]id", spotify_device_list.state.toString)
logInfo("TESTING", newValue2)
var newValue3 = newValue2.replace("[","")
logInfo("TESTING", newValue3)
newValue3 = newValue3.replace("]","")
logInfo("TESTING", newValue3)
spotify_device_ID_SHIELD.postUpdate(newValue3 )
spotify_device_ID_Kitchen.postUpdate( newValueX )
spotify_current_device_id.sendCommand(spotify_device_ID_SHIELD) //Shield
Thread::sleep(50)
spotify_action.sendCommand("transfer_playback")
I put
logInfo(“TESTING”, newValue2)
and that showed a value in the log,
2018-05-23 18:44:47.788 [INFO ] [lipse.smarthome.model.script.TESTING] - [“2546d8ce80580c473293d30e2c3477afaf985613”]
logInfo(“TESTING”, newValue3)
never showed up in the log - seems to be something wrong - what can I change?
rlkoshak
(Rich Koshak)
May 23, 2018, 8:02pm
4
I’m not certain this will work. There are some limitations with the JSONPATH implementation used by openHAB. But the [ ] indicates the value is in an array. So try
"$.[?(@.name=='Kitchen')]id[0]"
Did not work
2018-05-23 23:43:58.583 [INFO ] [lipse.smarthome.model.script.TESTING] - []
vzorglub
(Vincent Regaud)
May 23, 2018, 9:55pm
6
Can you post the json, please?
[{“name”: “SHIELD”, “volume_percent”: 86, “is_active”: false, “is_restricted”: false, “type”: “TV”, “id”: “2546d8ce80580c473293d30e2c3477afaf985613”}, {“name”: “Kitchen”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “2e2c5fe971a11069b23bdcb93d85919b487b8ff0”}, {“name”: “Jessicas room”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “556055d01765e703e54d1eadecafdc0a581132dd”}, {“name”: “Amazon FireTV Gen 2 \u2658”, “volume_percent”: 73, “is_active”: false, “is_restricted”: false, “type”: “TV”, “id”: “acb0b1b3c7b425eaba15ded489830415de5a99a2”}, {“name”: “Cinema”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “d4ce495101d24e0b718a55e974948a3df16ded89”}]
vzorglub
(Vincent Regaud)
May 23, 2018, 10:28pm
8
Your jsonpath should be $.[?(@.name=='SHIELD')].id
You need a .
after the ]
vzorglub
(Vincent Regaud)
May 23, 2018, 10:46pm
9
val newValue = transform("JSONPATH", "$.[?(@.name=='Kitchen')].id", spotify_device_list.state.toString)
spotify_device_ID_Kitchen.postUpdate( newValue )
val String newValue2 = transform("JSONPATH", "$.[?(@.name=='SHIELD')].id", spotify_device_list.state.toString)
logInfo("TESTING", newValue2)
spotify_device_ID_SHIELD.postUpdate(newValue2)
// spotify_device_ID_Kitchen.postUpdate( newValueX) ??
spotify_current_device_id.sendCommand(spotify_device_ID_SHIELD) //Shield
Thread::sleep(50)
spotify_action.sendCommand("transfer_playback")
$.[?(@.name==‘SHIELD’)].id gave the same result as $.[?(@.name==‘SHIELD’)]id
the [ ] is still there
Any other ideas?
Can I use regex to drop the first and last charracters? However, all my trials have failed
rlkoshak
(Rich Koshak)
May 24, 2018, 9:40pm
12
.*\[(.*)\].*
should work to strip off the brackets.
I cant get the rule to work
val newValue = transform("JSONPATH", "$.[?(@.name=='Cinema')].id", spotify_device_list.state.toString)
// spotify_device_ID_Kitchen.postUpdate( newValue )
val newValue2 = transform("JSONPATH", "$.[?(@.name=='SHIELD')]id", spotify_device_list.state.toString)
spotify_device_ID_SHIELD.postUpdate( newValue2 )
val newValue3 = transform("REGEX", ".*\[(.*)\].*", newValue.state.toString)
spotify_device_ID_SHIELD.postUpdate( newValue3 )
rlkoshak
(Rich Koshak)
May 24, 2018, 10:10pm
14
There are a whole lot of ways a rule can not work. What specifically are you getting? What errors do you see in openhab.log?
Note that you do not call .state on a variable. You only call .state on Items so change the regex trasform to:
val newValue3 = transform("REGEX", ".*\[(.*)\].*", newValue)
with
val newValue3 = transform(“REGEX”, “.[(. )].*”, newValue)
I get an error in SmartHome Designer
"Invalid escape sequence (valid ones are \b \t \n \f \r " ’ \)
something went wrong in the copying, this was the expression that was used in the rule
val newValue3 = transform("REGEX", ".*\[(.*)\].*", newValue)
vzorglub
(Vincent Regaud)
May 25, 2018, 7:06am
17
This works for me:
rule "jsontest"
when
Item jsontest received command
then
val newValue = transform("JSONPATH", "$.[?(@.name=='Cinema')].id", jsontest.state.toString)
logInfo("TEST", newValue)
end
2018-05-25 08:05:39.686 [ome.event.ItemCommandEvent] - Item 'jsontest' received command [{"name": "SHIELD", "volume_percent": 86, "is_active": false, "is_restricted": false, "type": "TV", "id": "2546d8ce80580c473293d30e2c3477afaf985613"}, {"name": "Kitchen", "volume_percent": 50, "is_active": false, "is_restricted": false, "type": "Speaker", "id": "2e2c5fe971a11069b23bdcb93d85919b487b8ff0"}, {"name": "Jessicas room", "volume_percent": 50, "is_active": false, "is_restricted": false, "type": "Speaker", "id": "556055d01765e703e54d1eadecafdc0a581132dd"}, {"name": "Amazon FireTV Gen 2 \u2658", "volume_percent": 73, "is_active": false, "is_restricted": false, "type": "TV", "id": "acb0b1b3c7b425eaba15ded489830415de5a99a2"}, {"name": "Cinema", "volume_percent": 50, "is_active": false, "is_restricted": false, "type": "Speaker", "id": "d4ce495101d24e0b718a55e974948a3df16ded89"}]
==> /var/log/openhab2/openhab.log <==
2018-05-25 08:05:39.696 [INFO ] [.eclipse.smarthome.model.script.TEST] - d4ce495101d24e0b718a55e974948a3df16ded89
This means you have problems with other parts of your rule.
Can you post the whole rule as you have it now, please?
rlkoshak
(Rich Koshak)
May 25, 2018, 1:10pm
18
ESHD is basically dead. It hasn’t been updated since 2.0 and will not be updated. VSCode with the openHAB extension is far superior.
Do you see the same error in openhab.log?
KevinHab
(Kevin Hab)
May 26, 2018, 2:59am
19
patrikpatrik2:
[{“name”: “SHIELD”, “volume_percent”: 86, “is_active”: false, “is_restricted”: false, “type”: “TV”, “id”: “2546d8ce80580c473293d30e2c3477afaf985613”}, {“name”: “Kitchen”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “2e2c5fe971a11069b23bdcb93d85919b487b8ff0”}, {“name”: “Jessicas room”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “556055d01765e703e54d1eadecafdc0a581132dd”}, {“name”: “Amazon FireTV Gen 2 \u2658”, “volume_percent”: 73, “is_active”: false, “is_restricted”: false, “type”: “TV”, “id”: “acb0b1b3c7b425eaba15ded489830415de5a99a2”}, {“name”: “Cinema”, “volume_percent”: 50, “is_active”: false, “is_restricted”: false, “type”: “Speaker”, “id”: “d4ce495101d24e0b718a55e974948a3df16ded89”}]
With your json, I tested and it work with rule below :
import java.lang.*
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
val json = "[{\"name\": \"SHIELD\", \"volume_percent\": 86, \"is_active\": false, \"is_restricted\": false, \"type\": \"TV\", \"id\": \"2546d8ce80580c473293d30e2c3477afaf985613\"}, {\"name\": \"Kitchen\", \"volume_percent\": 50, \"is_active\": false, \"is_restricted\": false, \"type\": \"Speaker\", \"id\": \"2e2c5fe971a11069b23bdcb93d85919b487b8ff0\"}, {\"name\": \"Jessicas room\", \"volume_percent\": 50, \"is_active\": false, \"is_restricted\": false, \"type\": \"Speaker\", \"id\": \"556055d01765e703e54d1eadecafdc0a581132dd\"}, {\"name\": \"Amazon FireTV Gen 2 \u2658\", \"volume_percent\": 73, \"is_active\": false, \"is_restricted\": false, \"type\": \"TV\", \"id\": \"acb0b1b3c7b425eaba15ded489830415de5a99a2\"}, {\"name\": \"Cinema\", \"volume_percent\": 50, \"is_active\": false, \"is_restricted\": false, \"type\": \"Speaker\", \"id\": \"d4ce495101d24e0b718a55e974948a3df16ded89\"}]"
rule "Read"
when
System started or
Time cron "0/10 * * * * ? *" //update every 10 seconds
then
var json_status = transform("JSONPATH", "$.[?(@.name=='SHIELD')].id", json) // filter json with name = SHIELD
var String json_TransformString = (json_status.replace('["', '').replace('"]', '')) // remove [ and ]
postUpdate(json_print, json_TransformString)
end
and the result :
09:52:10.177 [INFO ] [marthome.event.ItemStateChangedEvent] - json_print changed from NULL to 2546d8ce80580c473293d30e2c3477afaf985613
vzorglub
(Vincent Regaud)
May 26, 2018, 7:13am
20
Can you post the whole rule as you have it now, please?