Thank you for the comments.
a) thanks for the hint. I will use it in future.
b) I have a pull down resistor. But in testing my setup was simplified to:
only Arduino and relay module are connected together. Relay module is powered from arduinos +5v and GND. then pin 22 is connected to the relay module input. For test purpose I just gave +5V or GND from the same arduino board to the pin 23. And it still gives low to the output when I give it a +5V and High when GND is connected.
C) Done that and it functions as I need, but I want to understand how it works because this is the simplest part of the project and it's working not as I understand it..
As I wrote in test setup it's not a problem of floating switch. For the complete setup I soldered a group of pull down resistors.
And do you know is this internal pull up resistor exist for every one of 53 Arduino Mega inputs? And does it have some disadvantages against soldered pull down/up resistors? It is quite tempting to drop out some additional module that could cause some problems in future.
Thank you for the explanation and simplification of the code. Only one but.. as you say "The first code set y high if temp was high, and y low if temp was low - it sets y to the state of x" that's exactly what is not happening
it does the opposite and I don't understand why..