Home Assistant does not recognise the switch
-
I created a sketch to control a motorised projector screen of a home theatre. The sketch includes 3 relays that are connected to the up, down and stop buttons of the projector screen and a distance sensor to sense the screen. I used a rpi ethernet gateway running on the same rpi as home assistant. The sketch works correctly with MYSController. At the startup home assistant adds the node to to mysensors.json file. But I don't see the switch on Home Assistant default view / States screen / Services screen or logs. Can anyone help me.
Thanks in advance.
configuration.yaml
logger: default: info logs: mysensors: debug homeassistant.components.mysensors: debug mysensors: gateways: - device: '127.0.0.1' persistence_file: '/home/homeassistant/.homeassistant/mysensors.json' tcp_port: 5003 debug: true optimistic: true persistence: true #retain: true version: 2.0
mysensors.json:
{"0": {"sketch_name": null, "children": {}, "type": 18, "sensor_id": 0, "battery_level": 0, "sketch_version": null, "protocol_version": "2.1.1"}, "254": {"sketch_name": "ScreenController", "children": {"1": {"id": 1, "values": {}, "description": "", "type": 3}}, "type": 17, "sensor_id": 254, "battery_level": 0, "sketch_version": "1.0", "protocol_version": "2.1.1"}}
hass log (mysensors related entries):
17-01-29 07:37:41 INFO (MainThread) [homeassistant.loader] Loaded mysensors from homeassistant.components.mysensors 17-01-29 07:38:00 INFO (MainThread) [homeassistant.bootstrap] Setting up mysensors 17-01-29 07:38:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=mysensors> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.sensor> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.loader] Loaded sensor.mysensors from homeassistant.components.sensor.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.switch> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.light> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.binary_sensor> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.loader] Loaded switch.mysensors from homeassistant.components.switch.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.climate> 17-01-29 07:38:24 INFO (MainThread) [homeassistant.loader] Loaded light.mysensors from homeassistant.components.light.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.loader] Loaded binary_sensor.mysensors from homeassistant.components.binary_sensor.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.components.switch] Setting up switch.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.loader] Loaded climate.mysensors from homeassistant.components.climate.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.components.light] Setting up light.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.components.climate] Setting up climate.mysensors 17-01-29 07:38:24 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=switch, service=mysensors_send_ir_code> 17-01-29 07:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=, platform=mysensors, service=load_platform.cover> 17-01-29 07:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: discovered=name=mysensors, platform=mysensors, service=load_platform.notify> 17-01-29 07:38:25 INFO (MainThread) [homeassistant.loader] Loaded cover.mysensors from homeassistant.components.cover.mysensors 17-01-29 07:38:25 INFO (MainThread) [homeassistant.loader] Loaded notify.mysensors from homeassistant.components.notify.mysensors 17-01-29 07:38:25 INFO (MainThread) [homeassistant.components.cover] Setting up cover.mysensors 17-01-29 07:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=notify, service=mysensors> 17-01-29 07:38:25 INFO (Thread-17) [mysensors.mysensors] Trying to connect to ('127.0.0.1', 5003) 17-01-29 07:38:25 DEBUG (Thread-5) [homeassistant.components.mysensors] Update persistence: node 0 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 INFO (Thread-17) [mysensors.mysensors] Connected to ('127.0.0.1', 5003) 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 DEBUG (Thread-17) [mysensors.mysensors] Received 0;255;3;0;14;Gateway startup complete. 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 INFO (Thread-5) [homeassistant.components.mysensors] No sketch_name: node 0 17-01-29 07:38:25 DEBUG (Thread-5) [homeassistant.components.mysensors] Update persistence: node 254 17-01-29 07:58:01 DEBUG (Thread-17) [mysensors.mysensors] Received 254;255;3;0;21;0 17-01-29 08:18:02 DEBUG (Thread-17) [mysensors.mysensors] Received 254;255;3;0;21;0
Sketch:
// Enable debug prints to serial monitor //#define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <NewPing.h> // Enable repeater functionality for this node //#define MY_REPEATER_FEATURE #include <SPI.h> #include <MySensors.h> #define CHILD_ID 1 #define NUMBER_OF_RELAYS 3 // Total number of attached relays #define RELAY_UP 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_UP_ON 0 // GPIO value to write to turn on attached relay #define RELAY_UP_OFF 1 // GPIO value to write to turn off attached relay #define RELAY_DOWN 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_DOWN_ON 0 // GPIO value to write to turn on attached relay #define RELAY_DOWN_OFF 1 // GPIO value to write to turn off attached relay #define RELAY_STOP 7 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_STOP_ON 1 // GPIO value to write to turn on attached relay #define RELAY_STOP_OFF 0 // GPIO value to write to turn off attached relay // Sonar #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. #define SCREEN_DISTANCE 20 unsigned long SLEEP_TIME = 500; // Sleep time between reads (in milliseconds) NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. boolean lastVal = false; void before() { pinMode(RELAY_UP, OUTPUT); digitalWrite(RELAY_UP, RELAY_UP_OFF); pinMode(RELAY_DOWN, OUTPUT); digitalWrite(RELAY_DOWN, RELAY_DOWN_OFF); pinMode(RELAY_STOP, OUTPUT); digitalWrite(RELAY_STOP, RELAY_STOP_OFF); } void setup() { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("ScreenController", "1.0"); present(CHILD_ID, S_BINARY); } void loop() { } boolean isOpen() { int dist = sonar.ping_cm(); if (dist < 1) { return lastVal; } lastVal = (dist < SCREEN_DISTANCE); return lastVal; } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_STATUS) { boolean val = message.getBool(); if (val) { if (!isOpen()) { digitalWrite(RELAY_DOWN, RELAY_DOWN_ON); delay(SLEEP_TIME); digitalWrite(RELAY_DOWN, RELAY_DOWN_OFF); int x = 0; while(!isOpen() && x < 150) { x++; delay(SLEEP_TIME); } digitalWrite(RELAY_STOP, RELAY_STOP_ON); delay(SLEEP_TIME); digitalWrite(RELAY_STOP, RELAY_STOP_OFF); } } else { digitalWrite(RELAY_UP, RELAY_UP_ON); delay(SLEEP_TIME); digitalWrite(RELAY_UP, RELAY_UP_OFF); } // Write some debug info //Serial.print(message.sensor); } }
-
@erangaj You don't seem to send anything back from your sensor, I believe that is needed for HASS to add it... Spent a night fighting with HASS and similar issues but in the end I don't know exactly what fixed things though...
-
I converted it into a standard distance sensor with the sample distance sensor sketch. Now I can see the incoming messages on hass log as below. But the new sensor is not visible in any screen.
17-01-29 10:04:32 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:32 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;126 17-01-29 10:04:32 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:33 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;127 17-01-29 10:04:33 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:33 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;128 17-01-29 10:04:33 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:33 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;127 17-01-29 10:04:33 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:34 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;128 17-01-29 10:04:34 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:34 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;127 17-01-29 10:04:34 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:34 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;129 17-01-29 10:04:34 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1 17-01-29 10:04:35 DEBUG (Thread-17) [mysensors.mysensors] Received 1;1;1;0;13;127 17-01-29 10:04:35 DEBUG (Thread-17) [homeassistant.components.mysensors] Update sensor_update: node 1
-
Hi!
Yes, you need to present all children and send initial values. Each child (S_TYPE) must have at least one value (V_TYPE). Please read https://home-assistant.io/components/mysensors/#presentation.
If you look at what data home assistant has received from the device, node 254, in the persistence file, you can see that
values
is empty for child 1.If you want to use simple switches in home assistant for your three relays you should present three children, S_BINARY, and send a V_STATUS message as initial value for each child. I would suggest though that you look at the cover component and that mysensors platform. It will have UP, DOWN, and STOP buttons, instead of just on/off buttons.
https://home-assistant.io/components/cover.mysensors/
If you use that platform, you should only present one child of type S_COVER. You still need to send initial values. Look at the example sketch on the linked site. Take care not to have both up and down relays active at the same time, if you don't have hardware safety measures in place. That logic is missing in the sketch.
Another thing is that you shouldn't use
delay
in your sketch if you intend to receive messages. Usewait
instead for non battery powered devices. Duringwait
the mysensors library will process incoming messages in the background.Also, you can present the distance sensor as a separate child in the same sketch. Then you can use that for other automations in home assistant, eg sending notifications if left in half-open/half-closed mode.
-
@martinhjelmare Thank you for taking the time to write a very informative reply. It resolved the problem. At this stage my aim is to use it as a simple on / off type switch to control the screen from automation scripts. I used delay instead of wait since wait() didn't power up the radio automatically to receive messages. But with the modified script I use wait() since it sends a status message at the end and it wakes up the radio again.
-
@martinhjelmare Thanks, you post helped me in the right direction for a problem I had My hass also would not register a sensor and it's solved now.
-
@martinhjelmare Can the first/initial send() be done last in the presentation() function or does it have to be in the loop()?
-
It can't be done during
presentation
cause the node isn't registered yet at the gateway at that time. Default settings in mysensors requires nodes to be registered before the gateway allows them to send in values. In mysensors 2.1.xsetup
is called after registration, so it is possible to use that for sending values, but I would still recommend to do it in theloop
.Reason is, in some cases you want to resend the initial values, and then
setup
is not suitable as it's only called once. Eg if your're using the ethernet gateway and have local sensors on the gateway, when home assistant connects to the gateway, the gateway will re-runpresentation
and continue with theloop
. Butsetup
will not be called again, as it already has been called when the gateway was first started. So if you would send initial values of sensors insetup
on a gateway, the sensors wouldn't show in home assistant, cause the initial values would already be sent by the time home assistant connects to the gateway.Best way, I think, is to use an
if
block in the loop where you send the values and then request them from home assistant. You use a boolean variable, that you can turn on/off, that decides if the if block should be entered. You have the boolean True by default, and then set it to False when you have received the initial values back from home assistant inreceive
. This is described in the example sketch here:
https://home-assistant.io/components/mysensors/#presentation