Govee api command in a rule

i have a Govee H6199 tv backlight strip and trying to incorporate into my openhab just to turn on and off,I have found this Govee api ,i got an api key allright and i guess i have to use this example

Request URL: https://developer-api.govee.com/v1/devices/control
Request Method: PUT
Request Header:
Content-Type : application/json
Govee-API-Key : 00000000-729c-4b82-b536-000000000
Request body
{
"device": "34:20:03:15:82:ae",
"model": "H6089",
"cmd": {
"name": "turn",
"value": "on"
}
}

in a rule to turn on my stripā€¦any ideas?

If you want to send some HTTP, you would normally use the HTTP binding. Or HTTP Actions in a rule. Details vary by openHAB version in use.

i sure know that,i just dont know what syntax to use for the rule.I use http in rules like

rule "httpexample"
when
Item test received command
then
 sendHttpGetRequest("https://url to send to make something work")
end

i just dont know how to transform the Govee api example to one line url to use with ā€œsendHttpGetRequestā€ if possibleā€¦

You canā€™t, its a PUT and needs a message body (which you can build in a rule).

Repeating the hint, details vary by openHAB version in use.

i am using openhabian 2.5.12ā€¦

Here are the docs

An example with JSON

I think youā€™ll need toput your API key in headers
Example using headers here -

i came up with this rule based on my limited knowledge

rule "govee"
when
    Item test changed
then
    val url = "https://developer-api.govee.com/v1/devices/control"
	var String header = "Govee-API-Key:  0af6b18b-0303-4c87-b68a-17xxxxxxxxx"
    var String body = '{"device": "34:20:03:B6:xx:xx", "model": "H6199", "cmd": {"name": "turn","value": "on"}}'
    sendHttpPutRequest(url, header, "application/json", body)
end

but i get an error

2021-06-18 10:14:21.152 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'govee': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpPutRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null

For some reason, youā€™ve decided to give your parameters in a different order from the document, so itā€™s probably struggling to work what is which.
sendHttpPutRequest(url, contentType, content, headers, timeout)

Headers is special, headers is a Map type object, like a list or array, because it usually has multiple elements.
Thatā€™s why the example I showed you uses

When you are experimenting with this, itā€™s useful to get any response message. You need to give a timeout to collect that.

val putresponse = sendHttpPutRequest(url, contentType, body, headers, 10000)
logInfo("test", "PUT response: " + putresponse)

thnxā€¦one question, is it possible (and easier) to make a script with the document example and run it with

executeCommandLine("/etc/openhab2/scripts/govee.sh")

in my rule?

Sure. Donā€™t see why that would be any easier than a five-line rule, but just find out how to make HTTP requests in whatever scripting language you want to use. Remember you want to pass an on/off selection somehow.

thank you,i dont have the slightest idea about http requests ā€¦and http in general :slight_smile: i am trying to learn from docs and examplesā€¦

Okay, so probably best for now to stick with the docs and examples that you have. What does your rule look like now, is it working?

i am trying to do some work and to deal with a 4 yo :slight_smile: so later tonight i will give it a tryā€¦

I realise now I gave a poor example of ā€˜headersā€™ scripting - that was in javascript, not the DSL language youā€™re currently using.
Looking for a DSL example led me to this ā€¦

The upshot seems to be that headers are not supported in OH2 actions, and mention of them snuck into OH2 docs in error.

I donā€™t think this is going to work this way. You may be forced to use an external script after all (probably a bash script using curl).

got it thnx
i made this script named it govee.sh and put it in openHAB-conf\scripts folder

#!/bin/bash
curl 'https://developer-api.govee.com/v1/devices/control'--header'Govee-API-Key:0af6b18b-0303-4c87-b68a-xxxxxxxxxx'--header'Content-Type:application/json'--data-raw'{"device":"34:20:03:B6:xx:xx","model":"H6199","cmd":{"name":"turn","value":"on"}}

and test it with a rule

rule "govee"
when
	Item test received command
then
    executeCommandLine("/etc/openhab2/scripts/govee.sh")
end

and i get in the log

021-06-18 15:09:47.866 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/govee.sh'

but nothing really happens.

use the extended syntax
var RET = executeCommandLine("/etc/openhab2/scripts/govee.sh", 2000)
to get output returned. You may have to redirect the output from your curl command.
Alternatively you can redirect the output of the curl command to a temp. file within your shell script.

got it working thnx,my script is

#!/bin/bash
curl --location --request PUT 'https://developer-api.govee.com/v1/devices/control' --header 'Govee-API-Key: 0af6b18b-0303-4c87-b68a-xxxxxxxxx' --header 'Content-Type: application/json' --data-raw '{"device": "FF:AA:A4:C1:xx:xx:xx:xx","model": "H6199","cmd": {"name": "turn", "value": "off"}}'

no need for extended syntax.

1 Like

Thanks for sharing your solution. I also want to switch on and off a Govee H6199 with openhab. Also using openhabian 2.5.12.

Tried your way, but always get an error when running the script (of course I replaced Govee-API-Key and MAC-Adress):

{ā€œmessageā€:ā€œservice is busy please try again laterā€,ā€œstatusā€:500}

Any idea anyone?
Thx for helping.

You might be doing to same thing I did. For device name, even though it says its the mac address, its not. Do a GET on https://developer-api.govee.com/v1/devices with your api key header to find the actual address

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.