REST API from Fibaro Home Center 2 with LUA Script

Hi

I am trying to control some Telldus Live switches in Home Assistant form a Fibaro Home Center 2 with LUA scripting. When I look in the Web GUI of Home Assistant I can see that the switch changes state, but it changes back after about 5 seconds. I have noticed that the switch changes the normal “flash” to an on/off slider during these 5 seconds just to change back to the “flash” again.

This is the LUA code I use in my Fibaro:

local thisdevice = fibaro:getSelfId()
local ip = fibaro:getValue(thisdevice, 'IPAddress')
local port = fibaro:getValue(thisdevice, 'TCPPort')
local httpSession = Net.FHttp(ip, port);

jsonTable = { state = "off" }
jsonString = json.encode(jsonTable)

response, status, errorCode = httpSession:PUT('/api/states/switch.garage_motorvrmare', jsonString)

fibaro:debug("Response = "..response)
fibaro:debug("Status = "..status)
fibaro:debug("ErrorCode = "..errorCode)

Log Entry:

16-05-06 12:18:00
homeassistant.components.device_tracker.tomato.Tomato: Scanning

16-05-06 12:18:00
requests.packages.urllib3.connectionpool: Starting new HTTP connection (1):
192.168.1.1

16-05-06 12:18:02 homeassistant.core: Bus:Handling
<Event state_changed[L]: new_state=<state switch.garage_motorvrmare=off @
2016-05-06T12:18:02.581450+02:00>, entity_id=switch.garage_motorvrmare,
old_state=<state switch.garage_motorvrmare=on; friendly_name=Garage:
Motorvärmare, assumed_state=True @ 2016-05-06T12:16:30.506960+02:00>>

16-05-06 12:18:02 homeassistant.components.http:
"PUT /api/states/switch.garage_motorvrmare HTTP/1.1" 200 -

16-05-06 12:18:00 requests.packages.urllib3.connectionpool:
Starting new HTTP connection (1): 192.168.1.1

16-05-06 12:18:02 homeassistant.core: Bus:Handling
<Event state_changed[L]: new_state=<state switch.garage_motorvrmare=off @
2016-05-06T12:18:02.581450+02:00>, entity_id=switch.garage_motorvrmare,
old_state=<state switch.garage_motorvrmare=on; friendly_name=Garage:
Motorvärmare, assumed_state=True @ 2016-05-06T12:16:30.506960+02:00>>

16-05-06 12:18:02 homeassistant.components.http:
"PUT /api/states/switch.garage_motorvrmare HTTP/1.1" 200 -

16-05-06 12:18:02 homeassistant.core: Bus:Handling
<Event state_changed[L]: new_state=<state group.all_switches=off;
hidden=True, friendly_name=all switches, order=1, auto=True,
entity_id=('switch.hall_macro_dimmer', 'switch.sovrum_fnster', 'switch.garage_motorvrmare',
'switch.hall_sknk_vgguttag', 'switch.vardagsrum_vinstll_och_fglar',
'switch.biorum_bord_vgguttag', 'switch.utehumlebge', 'switch.tvttstuga_fnster',
'switch.hall_dimmer_tak', 'switch.hall_julgran_vgguttag',
'switch.gstrum_fnster_vgguttag', 'switch.sovrum_kattas_snglampa',
'switch.bl_rum_fnster_vgguttag', 'switch.vardagsrum_byr_och_tv',
'switch.hall_fnster', 'switch.trappa', 'switch.ute_trd', 'switch.sovrum_flkt',
'switch.vardagsrum_bursprk'), assumed_state=True @
2016-05-06T12:18:02.584347+02:00>, entity_id=group.all_switches,
old_state=<state group.all_switches=on; hidden=True, friendly_name=all
switches, order=1, auto=True, entity_id=('switch.hall_macro_dimmer',
'switch.sovrum_fnster', 'switch.garage_motorvrmare', 'switch.hall_sknk_vgguttag',
'switch.vardagsrum_vinstll_och_fglar', 'switch.biorum_bord_vgguttag',
'switch.utehumlebge', 'switch.tvttstuga_fnster', 'switch.hall_dimmer_tak',
'switch.hall_julgran_vgguttag', 'switch.gstrum_fnster_vgguttag',
'switch.sovrum_kattas_snglampa', 'switch.bl_rum_fnster_vgguttag',
'switch.vardagsrum_byr_och_tv', 'switch.hall_fnster', 'switch.trappa',
'switch.ute_trd', 'switch.sovrum_flkt', 'switch.vardagsrum_bursprk'),
assumed_state=True @ 2016-05-06T12:16:30.545694+02:00>>

16-05-06 12:18:09 netdisco.ssdp: Error fetching
description at http://172.16.1.10:50000/rootDesc.xml

16-05-06 12:18:09
requests.packages.urllib3.connectionpool: Starting new HTTP connection (1):
192.168.1.10

16-05-06 12:18:30 homeassistant.core: Bus:Handling
<Event state_changed[L]: new_state=<state sun.sun=above_horizon;
next_rising=2016-05-07T02:29:44+00:00, elevation=46.56,
next_setting=2016-05-06T18:59:51+00:00, friendly_name=Sun @
2016-05-06T12:15:40.565420+02:00>, entity_id=sun.sun, old_state=<state
sun.sun=above_horizon; next_rising=2016-05-07T02:29:44+00:00, elevation=46.54,
next_setting=2016-05-06T18:59:51+00:00, friendly_name=Sun @
2016-05-06T12:15:40.565420+02:00>>

16-05-06 12:18:30 homeassistant.core: Bus:Handling
<Event state_changed[L]: new_state=<state switch.garage_motorvrmare=on; friendly_name=Garage:
Motorvärmare, assumed_state=True @ 2016-05-06T12:18:30.678069+02:00>,
entity_id=switch.garage_motorvrmare, old_state=<state
switch.garage_motorvrmare=off @ 2016-05-06T12:18:02.581450+02:00>>

16-05-06
12:18:30 homeassistant.core: Bus:Handling <Event state_changed[L]:
new_state=<state group.all_switches=on; hidden=True, friendly_name=all
switches, order=1, auto=True, entity_id=('switch.hall_macro_dimmer',
'switch.sovrum_fnster', 'switch.garage_motorvrmare', 'switch.hall_sknk_vgguttag',
'switch.vardagsrum_vinstll_och_fglar', 'switch.biorum_bord_vgguttag',
'switch.utehumlebge', 'switch.tvttstuga_fnster', 'switch.hall_dimmer_tak',
'switch.hall_julgran_vgguttag', 'switch.gstrum_fnster_vgguttag',
'switch.sovrum_kattas_snglampa', 'switch.bl_rum_fnster_vgguttag',
'switch.vardagsrum_byr_och_tv', 'switch.hall_fnster', 'switch.trappa',
'switch.ute_trd', 'switch.sovrum_flkt', 'switch.vardagsrum_bursprk'),
assumed_state=True @ 2016-05-06T12:18:30.681804+02:00>,
entity_id=group.all_switches, old_state=<state group.all_switches=off;
hidden=True, friendly_name=all switches, order=1, auto=True,
entity_id=('switch.hall_macro_dimmer', 'switch.sovrum_fnster',
'switch.garage_motorvrmare', 'switch.hall_sknk_vgguttag',
'switch.vardagsrum_vinstll_och_fglar', 'switch.biorum_bord_vgguttag',
'switch.utehumlebge', 'switch.tvttstuga_fnster', 'switch.hall_dimmer_tak',
'switch.hall_julgran_vgguttag', 'switch.gstrum_fnster_vgguttag',
'switch.sovrum_kattas_snglampa', 'switch.bl_rum_fnster_vgguttag',
'switch.vardagsrum_byr_och_tv', 'switch.hall_fnster', 'switch.trappa',
'switch.ute_trd', 'switch.sovrum_flkt', 'switch.vardagsrum_bursprk'),
assumed_state=True @ 2016-05-06T12:18:02.584347+02:00>>

Any ideas on what I am doing wrong?

I have now found out that even when I use the Developer Tools - States I get the same issue that the Telldus Live switch changes in HA GUI for a few seconds and then changes back to previous state, and the physical switch never changes.

Or is this the default behaviour?

I have now found the solution and just wanted to share:

To turn ON a switch device:

local switch = “switch.tv_fonster” – Name of the light dimmer in Home Assistant
local thisdevice = fibaro:getSelfId()
local ip = fibaro:getValue(thisdevice, ‘IPAddress’)
local port = fibaro:getValue(thisdevice, ‘TCPPort’)
local httpSession = Net.FHttp(ip, port);

jsonTable = { entity_id = switch }
jsonString = json.encode(jsonTable)

response, status, errorCode = httpSession:POST("/api/services/switch/turn_on", jsonString)

To turn On a dimmer device:

local light = “light.tv_fonster_dim” – Name of the light dimmer in Home Assistant
local thisdevice = fibaro:getSelfId()
local ip = fibaro:getValue(thisdevice, ‘IPAddress’)
local port = fibaro:getValue(thisdevice, ‘TCPPort’)
local httpSession = Net.FHttp(ip, port);

local sliderValue = fibaro:getValue(thisdevice, “ui.Slider1.value”) – Get Fibaro Slider Value

if tonumber(sliderValue) <= 5
then
jsonTable = { entity_id = light, brightness = “100” }
elseif tonumber(sliderValue) <= 20
then
jsonTable = { entity_id = light, brightness = math.floor((sliderValue*2.55)+0.5) }
else
jsonTable = { entity_id = light, brightness = “100” }
end

jsonString = json.encode(jsonTable)
response, status, errorCode = httpSession:POST("/api/services/light/turn_on", jsonString)

If the dimmer is set to high or low it will revert back to 100 of the max 255.
But in Fibaro the max value is 100 so that is why I need to multiply with 2.55

Hi Svartbacken
I was wondering if you could add a little detail to turn on a switch command from Fibaro to the home assistant,

local switch = “switch.tv_fonster” – Name of the light dimmer in Home Assistant
local thisdevice = fibaro:getSelfId() --Does anything need to be added here?
local ip = fibaro:getValue(thisdevice, ‘IPAddress’) – is this the IP of the home assistant or the HC2?
local port = fibaro:getValue(thisdevice, ‘TCPPort’) – the port of the home assistant? or HC2?

local httpSession = Net.FHttp(ip, port); — Ip / port of the HC2 or home assistant?

jsonTable = { entity_id = switch } – anything needed here specific to my setup?
jsonString = json.encode(jsonTable)

response, status, errorCode = httpSession:POST("/api/services/switch/turn_on", jsonString) – anything needed here specific to my setup?

your response would be much appreciated.

To make it simple.

  1. Change the IP Address in the Virtual Device to match the IP of your Home Assistant.
  2. Change the TCP Port of the Virtual Device to match the TCP Port of Home Assistant.
  3. Change local switch = “switch.tv_fonster”, change switch.tv_fonster to the name of your switch in Home Assistant
  4. Done

i used your Lua config on my HC2 it did not work

Hi

It works for me and ShaunK. You don’t give me much to help you with :slight_smile:

Thanks a lot man for your prompt reply , it seems i was using on Scenes in Fibaro instead of Virtual devices ,

Yes, im noob when we are talking about lua code and home centers virtual devices…
Could you please attach a picture of a working virtual devices lua code?
I cant get it to work for some reason.
btw. i tried to enter home assistants username and password to virtual devices ip address.
Is that how you should do it? Looks like this: admin:Ko*********@192.168.1.75

Thank you!

I don’t use username and password om my HomeAssistant. I you do, you need another string.

1 Like

aaaaand it works!! Thank you very much! :slight_smile :slight_smile:
The problem was on the name of the switch (i didnt type the “switch” word) and propably the password too.
I removed the password because i dont need to be able to get in to my qnap(runs home assistant) from “outside” because my raspberry pi is running the home assistant too :slight_smile:

Anyway to read temperatures from home assistant…?

is there away to get climate component work with Fibaro home center , to control climate control unit in home assistant .