[SOLVED] Rules - Copy a data in a plc item

Hi,
I encounter some problem with “rules”. I’m beginner with openhab and especially with the rules.

I read a temperature value from a xiaomi aqara.

Number:Temperature HTP_Temperature { channel=“mihome:sensor_weather_v1:…:…:temperature” }

I would like to transfer the value to the plc (S7-1200)

Number XiaomiGw_TpHum_Z1_Tp “Salon - Temperature [%f]” { simatic=“plc:DB15.DBD0:float:IO” }

This is the rule:

rule “XiaomiGw_TpHum_Z1”
when
Time cron “0/15 * * * * ?”
then
var TeTemperature = HTP_Temperature.state as Number
XiaomiGw_TpHum_Z1_Tp.state = TeTemperature
logInfo("HTP_Temperature: ", HTP_Temperature.toString)
logInfo("XiaomiGw_TpHum_Z1_Tp: ", XiaomiGw_TpHum_Z1_Tp.toString)
end

This is the log info:

2019-07-14 21:24:00.028 [vent.ItemStateChangedEvent] - XiaomiGw_TpHum_Z1_Tp changed from 0.0 to 22.91 °C
2019-07-14 21:24:00.028 [INFO ] [thome.model.script.HTP_Temperature: ] - HTP_Temperature (Type=NumberItem, State=22.91 °C, Label=null, Category=temperature)
2019-07-14 21:24:00.036 [INFO ] [.model.script.XiaomiGw_TpHum_Z1_Tp: ] - XiaomiGw_TpHum_Z1_Tp (Type=NumberItem, State=22.91 °C, Label=Salon - Temperature, Category=temperature)
2019-07-14 21:24:00.466 [vent.ItemStateChangedEvent] - XiaomiGw_TpHum_Z1_Tp changed from 22.91 °C to 0.0

Looks that i copy “state” and not only the value…
Thanks for your help

This is not the documented way to update Item states. Use postUpdate()

Besides, if you are trying to transfer this to your PLC via the binding you will need to send it as a command, not an update.

Yep, got what you asked for, Because your HTP_Temperature Item is a Number:Temperature type it has units, and you will want to remove those to transfer the value to a plain Number type.

rule "XiaomiGw_TpHum_Z1"
when
   Item HTP_Temperature changed
then
   var TeTemperature = (HTP_Temperature.state as QuantityType<Number>).doubleValue
   XiaomiGw_TpHum_Z1_Tp.sendCommand(TeTemperature.toString)
end

thanks a lot for your explanation.
Your modification works properly.

I’m going to analyze the structure.

Thanks