I had to make some changes in NODERED and in the configurations.
Try with this script, replacing the $$REPLACE$$ with your data:
[
{
"id": "ea5358ba.a72db",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": ""
},
{
"id": "e62f416e.81e5a",
"type": "change",
"z": "ea5358ba.a72db",
"name": "set tuya_devices",
"rules": [
{
"t": "set",
"p": "tuya_devices",
"pt": "flow",
"to": "(\t $s1 := \"switch_1\";\t [\t {\t \"devId\" : \"$$REPLACE$$\",\t \"ip\" : \"$$REPLACE$$\",\t \"name\" : $s1\t }\t ]\t)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 330,
"y": 220,
"wires": [
[]
]
},
{
"id": "d4643f9a.5a9b1",
"type": "inject",
"z": "ea5358ba.a72db",
"name": "",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 150,
"y": 220,
"wires": [
[
"e62f416e.81e5a"
]
]
},
{
"id": "a0ec6bf5.425b78",
"type": "inject",
"z": "ea5358ba.a72db",
"name": "",
"props": [
{
"p": "payload",
"v": "tuya_devices",
"vt": "flow"
},
{
"p": "topic",
"v": "",
"vt": "string"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "tuya_devices",
"payloadType": "flow",
"x": 140,
"y": 300,
"wires": [
[
"ff6fd49a.41c5f"
]
]
},
{
"id": "ff6fd49a.41c5f",
"type": "split",
"z": "ea5358ba.a72db",
"name": "",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 290,
"y": 300,
"wires": [
[
"2abfbef4.bb35f2"
]
]
},
{
"id": "2abfbef4.bb35f2",
"type": "change",
"z": "ea5358ba.a72db",
"name": "set mqtt devices",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "(\t$topic_prefix := \"tuya\";\t$dev_name := payload.name;\t {\t \"platform\":\"mqtt\",\t \"name\": $dev_name,\t \"state_topic\":$topic_prefix & \"/\" & $dev_name & \"/status\",\t \"value_template\": \"{{ value_json.state }}\",\t \"command_topic\":$topic_prefix & \"/\" & $dev_name & \"/set\",\t \"availability_topic\":$topic_prefix & \"/\" & $dev_name & \"/available\",\t \"payload_available\":\"online\",\t \"payload_not_available\":\"offline\",\t \"json_attributes_topic\": $topic_prefix & \"/\" & $dev_name & \"/status\",\t \"json_attributes_template\": \"{{ value_json.attributes | tojson }}\",\t \"payload_on\":\"ON\",\t \"payload_off\":\"OFF\",\t \"optimistic\":false,\t \"qos\":0,\t \"retain\":false\t }\t)",
"tot": "jsonata"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "\"homeassistant/switch/\" & payload.name & \"/config\"",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 500,
"y": 300,
"wires": [
[
"a19c9891.9b331"
]
]
},
{
"id": "a19c9891.9b331",
"type": "mqtt out",
"z": "ea5358ba.a72db",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "8a631f32.0b624",
"x": 650,
"y": 300,
"wires": []
},
{
"id": "8ef6a354.d4f0b8",
"type": "tuya-local",
"z": "ea5358ba.a72db",
"devName": "switch_1",
"devIp": "$$REPLACE$$",
"devId": "$$REPLACE$$",
"devKey": "$$REPLACE$$",
"protocolVer": "3.3",
"renameSchema": "",
"filterCB": "",
"x": 660,
"y": 400,
"wires": [
[
"a6c78422.f3669",
"bb365c60.43ec68"
]
]
},
{
"id": "4ddba09.605bae",
"type": "mqtt in",
"z": "ea5358ba.a72db",
"name": "",
"topic": "tuya/+/set",
"qos": "2",
"datatype": "auto",
"broker": "8a631f32.0b624",
"x": 100,
"y": 520,
"wires": [
[
"2e01d468.10e464"
]
]
},
{
"id": "c22c6132.c7d808",
"type": "change",
"z": "ea5358ba.a72db",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "data.available ? \"online\" : \"offline\"",
"tot": "jsonata"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "\"tuya/\" & data.name & \"/available\"",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 980,
"y": 460,
"wires": [
[
"5d21a236.721bcc"
]
]
},
{
"id": "5d21a236.721bcc",
"type": "mqtt out",
"z": "ea5358ba.a72db",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "8a631f32.0b624",
"x": 1190,
"y": 420,
"wires": []
},
{
"id": "2e01d468.10e464",
"type": "change",
"z": "ea5358ba.a72db",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "(\t$new_state := (payload = \"true\") ? true : false; \t{ \"set\" : $new_state, \"dps\": 1 }\t)",
"tot": "jsonata"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "(\t$name:= $split(topic, \"/\")[1];\t $tuya:= [\t {\t \"devId\":\"$$REPLACE$$\",\t \"ip\":\"$$REPLACE$$\",\t \"localKey\":\"$$REPLACE$$\",\t \"name\":\"switch_1\",\t \"state_t\":\"medion/switch_1\",\t \"cmd_t\":\"medion/switch_1/set\",\t \"attr_t\":\"medion/switch_1/attributes\",\t \"avai_t\":\"medion/switch_1/available\",\t \"entity_id\":\"switch.medion_switch_1_mqtt\"\t }\t ];\t$device := $filter($tuya, function($x) {\t $x.name = $name\t});\t$device.ip\t)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 320,
"y": 440,
"wires": [
[
"8ef6a354.d4f0b8"
]
]
},
{
"id": "844a1f1d.87baa8",
"type": "function",
"z": "ea5358ba.a72db",
"name": "",
"func": "\nmsg.topic = \"tuya/\" + msg.data.name + \"/status\"\n \nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 950,
"y": 420,
"wires": [
[
"5d21a236.721bcc"
]
]
},
{
"id": "c400326b.65091",
"type": "comment",
"z": "ea5358ba.a72db",
"name": "set device information",
"info": "",
"x": 160,
"y": 160,
"wires": []
},
{
"id": "b0d0999b.5bccb8",
"type": "comment",
"z": "ea5358ba.a72db",
"name": "set homeassistant discovery",
"info": "",
"x": 180,
"y": 260,
"wires": []
},
{
"id": "7c2f1600.89fee8",
"type": "comment",
"z": "ea5358ba.a72db",
"name": "control-status",
"info": "",
"x": 130,
"y": 380,
"wires": []
},
{
"id": "a6c78422.f3669",
"type": "function",
"z": "ea5358ba.a72db",
"name": "",
"func": "if (\"payload\" in msg) {\n return [msg,msg];\n} else {\n return [null,msg];\n}",
"outputs": 2,
"noerr": 0,
"x": 800,
"y": 540,
"wires": [
[
"844a1f1d.87baa8"
],
[
"c22c6132.c7d808"
]
]
},
{
"id": "bb365c60.43ec68",
"type": "debug",
"z": "ea5358ba.a72db",
"name": "teste",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 620,
"y": 600,
"wires": []
},
{
"id": "8a631f32.0b624",
"type": "mqtt-broker",
"z": "",
"name": "hass",
"broker": "$$REPLACE$$",
"port": "1883",
"clientid": "nr-docker",
"usetls": false,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": ""
}
]
And with this configurations in the switch:
- platform: mqtt
name: "Aquecedor"
state_topic: "tuya/switch_1/status"
value_template: "{{ value_json.dps['1'] }}"
command_topic: "tuya/switch_1/set"
payload_on: "true"
payload_off: "false"
state_on: "True"
state_off: "False"
optimistic: false
qos: 0
retain: true
and this in the sensors:
- platform: mqtt
name: "Aquecedor_Power"
state_topic: "tuya/switch_1/status"
unit_of_measurement: W
value_template: "{{ value_json.dps['19'] | multiply(0.1) | float(2) | round (2) }}"
- platform: mqtt
name: "Aquecedor_Current"
state_topic: "tuya/switch_1/status"
unit_of_measurement: mA
value_template: "{{ value_json.dps['18'] | float(2) | round(2) }}"
- platform: mqtt
name: "Aquecedor_Voltage"
state_topic: "tuya/switch_1/status"
unit_of_measurement: V
value_template: "{{ value_json.dps['20'] | multiply(0.1) | float(2) | round (2) }}"